/* Bismillahir Rahmanir Rahim
Sieve-all primes from 1 to n
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool ar[10000009];
void sieve(ll x){
for(int i=3; i*i<=x; i+=2){
if(ar[i]){
for(int j=i*3; j<=x; j+=i*2) ar[j]=false;
}
}
}
int main(){
ll n, k;
while(cin>>n){
memset(ar, true, sizeof(ar));
sieve(n);
cout<<2<<" ";
for(k=3; k<=n; k+=2){
if(ar[k])cout<<k<<" ";
}
cout<<endl;
}
return 0;
}
|
Monday, March 20, 2017
Sieve-All Primes from 2 to n
Subscribe to:
Post Comments (Atom)
-
#include<bits/stdc++.h> #define ll long long using namespace std ; ll n , k , t_case ; ll bigmod ( ll b , ll p , ll m...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3...
No comments:
Post a Comment