Summary of Quality Quantity

Source: Internet
Author: User

Prime number. In a natural number greater than 1, except for 1 and the integer itself, it cannot be divisible by other natural numbers.
1. Determine whether a number is a prime number

First, according to the definition, the simplest way to determine whether a number N is a prime number is to determine whether N can be divisible from 2 to all the numbers smaller than N, if neither of them can be divisible, It is a prime number.

bool IsPrime(int n){if(n == 1)return false;for(int i = 2; i < n; i++)if(n % i ==0)return false;return true;}

Let's make a slight improvement. We can first judge whether 2 can divide N, and then the even numbers above 2 do not need to be judged again, because 2 cannot divide N, then the number containing factor 2 certainly cannot divide n. Furthermore, we do not need to loop to N at all. If the product of the two numbers is N, there must be a factor smaller than SQRT (N). Therefore, the maximum number of loops can be SQRT (n ). So there is:

bool IsPrime(int n){if(n == 2)return true;if(n == 1 || (n & 1))return false;int k = (int)sqrt(n);for(int i = 3; i <= k; i += 2)if(n % i == 0)return false;return true;}

The above filters out the multiples of 2, and the calculation amount is changed to the original 1/2. We can also filter out the multiples of 3, that is, only judge whether 3 can divide n, the number of multiples of the other three does not need to be determined, and the calculation amount can be changed to 1/3 of the original. That is, only 6 * k + 1 and 6 * k + 5 (k is an integer) are left, because other numbers contain either factor 2 or 3, if n cannot be divisible by 2 or 3, N cannot be divisible by 6 * k, 6 * k + 2, 6 * k + 3, 6 * k + 4, you do not need to judge whether these numbers can be divided by N, which reduces the number of judgments.

bool IsPrime(int n){if((n == 2) || (n == 3))return true;if((n == 1) || ((n & 1) == 0) || ((n % 3) ==0))return false;int k = (int)sqrt(n);int step = 4;for(int i = 5; i <= k; i += step){if(n % i == 0)return false;step = 6 - step;}return true;}

2. Find all prime numbers in the interval
It is required that all the prime numbers in the interval be searched one by one using the above method, but it is too slow...
This type of problem mainly involves the limit method. The main idea of the sieve method for determining the prime number is to remove the multiples (including factor K) of all the following K after judging whether a number K is a prime number from small to large.

# Define n 1000000 bool isprime [N]; int I; memset (isprime, true, sizeof (isprime); isprime [1] = false; // screen out an even number greater than 2 for (I = 4; I <n; I + = 2) isprime [I] = false; int bound = SQRT (N ); // use the prime number within SQRT (n) to screen out the element containing the prime number factor (I = 3; I <= bound; I + = 2) {// the odd number starting from 3. If it is a prime number, filter out the element with its own factor, instead of skipping if (false = isprime [I]) continue; int step = I <1; // I is the prime number, then the screen starts from I * I and takes 2 * I as the step size (because the even number has been filtered out, for (Int J = I * I; j <n; j + = step) // contains prime factor I. isprime [J] = false is filtered out ;}

If you want to calculate the prime number between two large numbers, you can also use the screening method.
For example, the prime number between N1 and N2 is 1000000000 <= N1 <N2 <2 ^ 32 and 1 <N2-N1 <1000000

Bool isprime [1000000]; int prime [70000]; // store the prime number smaller than 65536 (the number within the unsigned int range can be sifted), and the int primecnt can be obtained by sifted; // Number of prime numbers less than 65536 int I, j;/* calculate the number of prime numbers less than 65536 and save it in the prime array */INT n1 = 100000000; int n2 = 101000000; int d = n2-N1; memset (isprime, true, sizeof (isprime); for (I = 0; I <primecnt; I ++) {int K = n2 % prime [I]; // The N2-k prime [I], not prime, n2-K-x * prime [I] (X is an integer) are not prime numbers, and INT step = prime [I] is filtered out; Int J = D-K; // N2-K corresponds to the array subscript D-K, n2-K-x * prime [I] corresponds to the array subscript J-x * prime [I]; while (j> = 0) {isprime [J] = false; // isprime [J] indicates whether N1 + J is a prime number J-= step ;}}

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: 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.