The realization of the algorithm of prime number judgement _c language

Source: Internet
Author: User

1. Determining the number of primes

The problem of determining prime number is a very common problem, this paper introduces several common methods of judging.

2. Original algorithm

A prime is defined as a number that, apart from being divisible by 1 and itself, cannot be divisible by any number of other numbers. According to the definition of prime number only need to use 2 to n-1 to remove N, if all is not, then n is prime, otherwise, as long as one of the numbers can divide the n is not prime.

Copy Code code as follows:

BOOL Is_primer1 (int num) {

int i;

for (i = 2; i < num; i++) {

if (num% i = = 0) {

return true;

}

}

return false;

}

3. Improved algorithm

N is not a prime number, then n can be represented as a*b, where 2<=a<=b<=n-1, then there must be a few in a,b: 1<x<=sqrt (n), so you just need to remove n with 2~SQRT (n), so you get a complex O (sqrt ( n)) algorithm

Copy Code code as follows:

BOOL Is_primer2 (int num) {

int i;

int upper = sqrt (num);

printf ("primer2:%d\n", Upper);

for (i = 2; i <= upper; i++) {

if (num% i = = 0) {

return true;

}

}

return false;

}

4. Filtering algorithms

A more efficient method of determining prime numbers should be to save prime numbers in a list of prime numbers, when judging whether a number is prime, direct look-up table can be. This approach needs to address two issues:

(1) How to quickly get the prime number table? (using a screening method)
(2) How to reduce the size of the prime table? (using bitmap data structure)

For 1 to n all integers, determine whether they are prime, find a non prime, dig it off, and the last is the prime. The specific methods are:

<1> definition Is_primer[i] = true;
<2> starts at 2, iterates through the entire is_primer (until sqrt (N)), and if Is_primer[i]=true, Is_primer[n*i]=false
such as 1,2,3,4,5,6,7,8,9,10, then
Traverse from 2:
Is_primer[2]=true, then is_primer[4]= is_primer[6]= is_primer[8]= is_primer[10]= True
Is_primer[3]=true, then is_primer[6]= is_primer[9]= true
In order to reduce the memory usage, the algorithm uses the bitmap data structure, regarding the bitmap, may refer to: http://www.jb51.net/article/54439.htm

Copy Code code as follows:

BOOL Load_primer_table1 () {//Save Prime Number Table

int i;

for (i = 1; i < Int_max; i++) {

if (i% 2!= 0//Even number must not be prime

&& Is_primer2 (i)) {

Set (i);

}

}

}

BOOL Load_primer_table2 () {//Another faster way to save prime numbers table

int I, J;

for (i = 1; I <= Int_max; i++) {

if (i% 2) {

Set (i);

} else {

Clear (i);

}

}

int upper = sqrt (Int_max);

for (i = 1; i <= upper; i++) {

if (Test (i)) {

for (j = i + i; j < Int_max; j = i)

Set (i);

}

}

}

BOOL Is_primer3 (long num) {//look-up table to determine whether a prime

if (Test (num))

return true;

return false;

}

5. Optimized filtering algorithm

(1) Storage mode optimization

Still using bitmap storage, only the odd number in the bitmap, which saves half of the space (the required space is only 4g/(32*2) =64MB)
After the storage space optimizes, the algorithm efficiency also can raise many, like: 1,2,...,30
Just store 3,5,7,9,11,13,15,17,19,21,23,25,27,29
I=0, Is_primer[0] =true, place subscript [3][6][9][12], that is, 9,15,21,27, Mark False
I=1, s_primer[0] =true, subscript as [6][11], that is, the 15,25 is marked false
i=2, 2*i+3>sqrt (30), end
Namely: I=s, labeled as S (2*t+1) +3t, of which, t=1,2,3, ... All of the Is_primer are set to False

(2) Optimized deletion algorithm

A is a prime, then the next starting point is the a*a, the back of all the a*a+2*i*a screen out. The prime number within n is desired, the prime number in the SQRT (n) is first obtained, and the number of the following numbers is screened with the prime number already obtained.

6. Summary

So far, no one has found the distribution of prime numbers, and no one can use a formula to calculate all the primes. A lot of interesting properties about primes or the efforts of scientists, such as:

(1) Gauss guesses that the number of pixels within n is about the same as N/LN (n), or that when n is large, the order of magnitude is the same. This is the famous theorem of prime numbers.

(2) 17th century horse speculation, 2 of the 2^n of the second party +1,n=0,1,2 ... When is prime, such numbers are called Fermat Prime, but when n=5, 2^32+1 is not prime, and so far did not find the sixth number of horse prime.

(3) The largest prime number discovered in 18th century was the largest prime number discovered in the 2^31-1,19 century, the largest known 2^127-1,20 in the late 2^859433-1, in decimal notation, which is a 258,715-digit figure.

(4) The twin prime guess: the prime number with a difference of 2 has infinitely many pairs. The biggest twin primes currently known are 1159142985x2^2304-1 and 1159142985x2^2304+1.

(5) Goethe's conjecture: All even numbers greater than 2 are the sum of two primes, and all odd numbers greater than 5 are the sum of three primes. The second conjecture is the first natural deduction, so the Goethe-Bach conjecture is called the 1+1 problem. Our mathematician Chen Jingrun proved the 1+2 that all even numbers greater than 2 are the sum of a prime number and a composite number with only two prime factors. Internationally known as the Chen's theorem.

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.