first, the concept of prime numbersprime number, also known as prime number, has an infinite number. A natural number greater than 1, except 1 and itself, cannot be divisible by other natural numbers (prime numbers), in other words, the number has no other factor other than 1 and itself; otherwise called composite. according to the basic theorem of arithmetic, each integer greater than 1 is either a prime number or a product of a series of prime numbers, and the written form is unique if the order of the primes is not considered in the product. The smallest prime number is 2.
second, the algorithmalgorithm 1.Open Root method: If a number (>2), the number of square root, if the number can be the square root of the number of any one between 2 (as long as there is a line) is not prime, if not the description is prime! principle: If a number N is composite, it has an approximate a,axb=n, then A, b two number must have a greater than or equal to the square root of N, a less than or equal to the square root of N. Therefore, as long as the number less than or equal to the square root n (except 1) cannot be divisible by n, then n must be prime.
public void Zhishu (int num) {int I, J, k;for (i = 2; i < num; i++) {k = (int.) math.sqrt (i); for (j = 2; J <= K; j + +) {if (i%j==0) {break;}} if (j>k) {System.out.println ("prime:" +i);}}}
Algorithm 2.Eratosthenes Prime number Screening method
to get all the primes within the natural number n, it must be less thanThe multiples of all primes are removed, and the remainder is the prime number.give the range N of the value to be sifted to find the prime number within. First use 2 to sift, that is, 2 left, the number of 2 is removed, and then the next prime number, that is, 3 sieve, 3 left, the multiples of 3 is removed, then the next prime number 5 sieve, 5 left, the multiples of 5 are removed; StepsThe following algorithms are listed in detail:
- List all the sequences after 2:
- 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
- Mark the first prime in the sequence, which is 2, and the sequence becomes:
- 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
- In the remaining sequence, the multiples of 2 are crossed out, and the sequence becomes:
- 2 3 5 7 9 11 13 15 17 19 21 23 25
- If the maximum number in this sequence is less than the sum of the last number of primes, then all the numbers in the remaining sequence are primes, otherwise return to the second step.
- In this case, since 25 is greater than 2 squared, we return to the second step:
- The first prime number in the remainder of the sequence is 3, the multiple of 3 in the main sequence is crossed out, and the main sequence becomes:
- 2 3 5 7 11 13 17 19 23 25
- The primes we get are: 2,3
- 25 is still greater than 3 squared, so we also return to the second step:
- The first prime number in the sequence is now 5, and the number of 5 in the sequence is also crossed out, and the main sequence is:
- The primes we get are: 2,3,5.
- Because 23 is less than 5 squared, jump out of the loop.
Conclusions: The prime number between 2 and 25 is: 2 3 5 7 11 13 17 19 23.
The following code is excerpted from the network
public static void Getprimes (int n) {if (N < 2 | | n > 9999999)//The maximum limit is 99,999,990,000 because of the JVM memory limit, of course there are other flexible schemes to bypass (such as Bitmap method) T Hrow New IllegalArgumentException ("Input parameter n Error! "); int[] array = new Int[n]; Assuming that all initial numbers are primes, and that a number is a prime, the value is 0, i.e. the first number is a prime and array[0] is 0array[0] = 1; 0 is not a prime array[1] = 1; 1 is not a prime number//The following is a filter core process for (int i = 2; i < MATH.SQRT (n); i++) {//starting from the minimum prime number 2 if (array[i] = = 0) {for (int j = i * I; J < N J + = i) {array[j] = 1;//identifies the position as a non-prime}}}//print N of all prime numbers, each row of 10 output System.out.println (n + "within the prime number as follows:"); int count = 0; The number of characters that are currently output int rowlength = 10; Number of characters per line output for (int i = 0; i < Array.Length; i++) {if (array[i] = = 0) {if (count% Rowlength = = 0 && Count! = 0) {System.out.println ();} count++; System.out.print (i + "\ t");}}}
Use the above two algorithms to find all the primes of 9999999Open Radical Method time: 21507 MsEratosthenes Prime number Screening time: 6658 Ms
with respect to algorithms, the following are some of the first mark down that are implemented in C.
http://blog.chinaunix.net/uid-26548237-id-3364131.html
Ultra-high-speed computing n number of prime numbers (within tens of 3 milliseconds to resolve) http://blog.csdn.net/jianxia_wzx/article/details/8663759
==================================================================================================
Ouyangpeng welcome reprint, sharing with people is the source of progress!
Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng
==================================================================================================
My Java Development learning journey------> Find all primes in n