1013 number prime numbers (20 minutes)
So that P i represents the i-th prime number. The current to the two positive integers M ≤ N ≤ . 1 0 . 4 , please output P M to P N all prime numbers.
Input formats:
In the given input row M and N, separated by a space therebetween.
Output formats:
Output from the P M to P N all prime numbers, each representing a digit line 10, separated by spaces therebetween, the end of the line may not have extra space.
Sample input:
5 27
Sample output:
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103
Ideas: the use of "Eppendorf" sieve
Memory: 1,000,000 (10 ^ 6) of the prime number 78498, 10000000 (10 ^ 7) of the prime number 664579
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std ; 6 7 const int N = 10000010 ; 8 9 bool st[N] ; 10 int primes[N] ; 11 int n, m ; 12 13 14 int main(){ 15 cin >> n >> m ; 16 17 int idx = 0 ; 18 19 for(int i=2;i<10000010;i++){ 20 if(!st[i]){ 21 primes[idx++] = i ; 22 if(idx == m){ 23 break ; 24 } 25 for(int j=i;j<=10000010;j += i){ 26 st[j] = true ; 27 } 28 } 29 } 30 idx = 0 ; 31 for(int i=n-1;i<m;i++){ 32 idx ++ ; 33 if(idx==10 || i==m-1){ 34 printf("%d\n",primes[i]) ; 35 idx = 0 ; 36 }else{ 37 printf("%d ",primes[i]) ; 38 } 39 } 40 41 return 0 ; 42 }