1675 Large prime numbers 2
time limit: 1 sspace limit: KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle Description
Description
Xiao Ming was stand by the maths teacher for not doing his homework, and then the maths teacher asked him to go home and find the nth prime number.
Xiaoming then hand over to the wise you. Ask for your help! "Wikioi-1530"
.............................. The above is the background ......... ...........
The teacher suspected that Xiao Ming was only to find the nth prime number, and then called Xiaoming to find all the prime numbers within 1 to n (excluding N) . Xiao Ming has found you again ...
Enter a description
Input Description
A positive integer n.
(1<=n<=1000000)
Output description
Output Description
Prime numbers within N, each row.
Sample input
Sample Input
233
Sample output
Sample Output
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229//(not including n=233)
Euler sieve method (Linear sieve method):
1#include <iostream>2 using namespacestd;3#include <cstdio>4 #defineN 10010005#include <bitset>6Bitset<n>check;7 intprim[n],tot=0;8 intMain ()9 {Ten intN; OneCin>>N; ACheck.Set(); - for(intI=2; i<n;++i) - { the if(Check[i]) - { -printf"%d\n", i); -prim[tot++]=i; + } - for(intj=0; j<tot;++j) + { A if(prim[j]*i>=n) Break; atcheck[prim[j]*i]=false; - if(i%prim[j]==0) Break; - } - } - - return 0; in}
Ed Sieve Method:
1 /*/ * This program overflowed at more than 40,000 i==, and Euler sieve method because there is "if (i%prim[j]==0) break;2 ", so generally does not multiply overflow*/*/3#include <iostream>4 using namespacestd;5#include <cstdio>6 #defineN 10010007#include <bitset>8Bitset<n>check;9 intprim[n],tot=0;Ten intMain () One { A intN; -Cin>>N; -Check.Set(); the for(intI=2; i<n;++i) - { - if(Check[i]) - { +printf"%d\n", i); - for(intj=i*i;j<n;j+=i) +check[j]=false; A } at - } - - return 0; -}
Prime number sieve Codevs 1675 large prime number 2