Analysis of python prime number embedding method and python Prime Number Analysis

Source: Internet
Author: User

Analysis of python prime number embedding method and python Prime Number Analysis

Principle:

Prime Number refers to the number in a natural number greater than 1 that cannot be divisible by any other natural number except 1 and itself. It plays an important role in encryption applications. For example, in the well-known RSA algorithm, it is a type-based Decomposition Problem Based on big integers, finding two super-large prime numbers and then multiplying them as keys. A common method for prime numbers isThe Sieve of Eratosthenes)To put it simply, draw a table and delete the table ,:

Start from 2 and start from 2. If the current number is a prime number, delete or mark all its multiples from the table, and finally obtain all the prime numbers.

There is an optimization:

When the multiples of 2 and 3 are marked, 6 is marked twice. Therefore, starting from the square of I, we can reduce the time.

For example, 3 is marked from 9, instead of 6, and 6 is added each time.

All prime numbers except 2 are odd. The square of an odd number is still an odd number. If an odd number is added, the even number is not a prime number. Therefore, an even number is added (2 times the prime number ).

All the even numbers are pre-processed.

Note: 1 is neither a prime number nor a union number, and 1 is not processed here.

#! prime.py import time  def primes(n):  P = []  f = []  for i in range(n+1):   if i > 2 and i%2 == 0:    f.append(1)   else:    f.append(0)   i = 3  while i*i <= n:   if f[i] == 0:    j = i*i    while j <= n:     f[j] = 1     j += i+i   i += 2   P.append(2)  for i in range(3,n,2):   if f[i] == 0:    P.append(i)   return P  def isPrime(n):  if n > 2 and n%2 == 0:   return 0   i = 3  while i*i <= n:   if n%i == 0:    return 0   i += 2   return 1  def primeCnt(n):  cnt = 0  for i in range(2,n):   if isPrime(i):    cnt += 1  return cnt  if __name__ == '__main__':  start = time.clock()  n = 10000000  P = primes(n);  print("There are %d primes less than %d"%(len(P),n))  #for i in range(10):  # print(P[i])  print("Time: %f"%(time.clock()-start))  #for n in range(2,100000):  # if isPrime(n):  #  print("%d is prime"%n)   #print("%d is "%n + ("prime" if isPrime(n) else "not prime"))   start = time.clock()  n = 1000000  print("There are %d primes less than %d"%(primeCnt(n),n))  print("Time: %f"%(time.clock()-start) 

5.767 s is used to calculate a prime number of less than 10 million using the prime number embedding method,

The ordinary prime number embedding method uses 9.642 s to calculate a prime number within 1 million,

The prime number less than 0.1 billion is obtained by using the prime number embedding method of C ++, and 0.948 s is used,

3.965 s is used to calculate a prime number of less than 10 million using the C ++ common prime number embedding method,

The explanation language is indeed much slower than the compilation language.

Attaches a C ++ program and uses bit compression to optimize the space.

#include <iostream> #include <cstdio> #include <algorithm> using namespace std; #define N 100000001  unsigned f[(N>>5)+5]; int p[5761456],m; void init() {   int i,j;   for(i=4;i<N;i+=2)     f[i>>5]|=1<<(i&0x1F);   p[m++]=2;   for(i=3;i*i<N;i+=2)     if(!(f[i>>5]&(1<<(i&0x1F))))     {       p[m++]=i;       for(j=i*i;j<N;j+=i+i)         f[j>>5]|=1<<(j&0x1F);     }   for(;i<N;i+=2)     if(!(f[i>>5]&(1<<(i&0x1F))))       p[m++]=i; } int is_prime(int n) {   int i;   for(i=0;p[i]*p[i]<=n;i++)     if(n%p[i]==0)       return 0;   return 1; } int isPrime(int n) {   if(n>2 && n%2==0)     return 0;   int i=3;   while(i*i<=n)   {     if(n%i==0)       return 0;     i+=2;   }   return 1; } int main() {   int n=0,i;   clock_t st=clock();   init();   /*for(i=2;i<10000000;i++)     if(isPrime(i))       n++;*/   printf("%d %dms\n",m,clock()-st);   /*while(~scanf("%d",&n),n)   {     i=lower_bound(p,p+m,n+1)-p;     printf("%d\n",i);   }*/   return 0; } 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.