The first is that the double for cycle makes the calculation of divisors and dividends extremely inefficient.
public void test1(int n){ long start = System.currentTimeMillis(); //Take the start time int num=0; boolean sign; for(int i=2;i<n;i++){ if(i % 2 == 0 && i != 2 ) continue; //Even Number and 1 Exclusion sign=true; for (int j=2;j<i;j++){ if(i%j==0){ sign=false; break; } } if (sign){ num++; /* System.out.println(""+i);*/ } } System.out.println(n+"The prime numbers within are"+num+"individual"); long end = System.currentTimeMillis(); System.out.println("The time cost is " + (end - start)); System.out.println(""); }
The second kind: mainly consider the number between 2 and i/2, the efficiency is half higher than the first kind.
public void test2(int n){ long start = System.currentTimeMillis(); //Take the start time int num=0; int j; boolean sgin; for (int i = 2; i <= n; i++) { if(i % 2 == 0 && i != 2 ) continue; //Even Number and 1 Exclusion sgin = true; for (j = 2; j <= i/2 ; j++) { if (i % j == 0) { sgin= false; break; } } //Printing if (sgin) { num++; /* System.out.println(""+i);*/ } } System.out.println(n+"The prime numbers within are"+num+"individual"); long end = System.currentTimeMillis(); System.out.println("The time cost is " + (end - start)); System.out.println(""); }
Third: Use the prescription to filter Math.sqrt(i)
public void test3(int n){ long start = System.currentTimeMillis(); //Take the start time int num=0; int j; boolean sgin; for (int i = 2; i <= n; i++) { if(i % 2 == 0 && i != 2 ) continue; //Even Number and 1 Exclusion sgin= true; for (j = 2; j <= Math.sqrt(i) ; j++) { if (i % j == 0) { sgin = false; break; } } //Printing if (sgin) { num++; /* System.out.println(""+i);*/ } } System.out.println(n+"The prime numbers within are"+num+"individual"); long end = System.currentTimeMillis(); System.out.println("The time cost is " + (end - start)); System.out.println(""); }
Fourth: Reverse thinking screening quality, the most efficient
public void test4(int n){ long start = System.currentTimeMillis(); //Take the start time //Sum of prime numbers int sum = 0; //1000 All prime numbers within ten thousand //Arrays are used to divide the numbers less than 10 million into two main factions. The prime number replaces the numerical value with 0, and the sum number replaces the numerical value with 1. //The default is all prime at first, so the values are all 0. When the screening starts, we assign the sum to 1. int num[] = new int[n]; num[0] = 1; //Since 1 is not a prime number, it is necessary to use 1 value in advance. //According to the conclusion of Ehrlich sieve method, natural numbers should be obtained. N All prime numbers within the limit must be less than or equal to" The two root number N "When all the prime multiples are removed, all that remains is the prime. double prescription = Math.sqrt(n); for (int i = 2; i <= prescription; i++) { //Start eliminating all prime multiples, and all that's left is prime. for (int j = i*i; j <= n; j+=i) { //from i*i Start removing because of the ratio i*i Small multiples have been removed before. //For example: i=5 //5 Two times (10), three times (15), in i=2 By the time, it had been removed. num[j-1] = 1; //Eliminate the multiple of the prime number, that is, assign a value of 1, either prime or sum. } } //Traversing the array, counting all the numbers with a value of 0, we can get the sum of prime numbers. for (int i = 0; i < num.length; i++) { if(num[i]==0) sum++; } System.out.println(n+"The prime numbers within are"+sum+"individual"); long end = System.currentTimeMillis(); System.out.println("The time cost is " + (end - start)); System.out.println(""); }
public static void main(String[] args) { Demo1 demo1 = new Demo1(); demo1.test1(100000); demo1.test2(100000); demo1.test3(100000); demo1.test4(100000); }
Result:
Note: Thanks for the learning resources provided by our predecessors. The first three are from: https://blog.csdn.net/u010503822/article/details/78734371 The last way is from: http://how2j.cn/k/number-string/number-string-math/319.html#nowhere
Please forgive me for any offence or mistake