Saturday, November 26, 2016

Sieve of Eratosthenes

#include<iostream>
#include<cstdio>

int N=100000;
int status[100001];
int main(){
    int i,j;
    for(i=2; i<=N; i++) status[i]=0;
    for(i=3; i<=N; i+=2) {
        if(status[i] == 0) {
            for(j = 2* i; j<=N; j+=i) status[j] = 1;
        }
    }
    for( i=2; i<=N; i++) if(status[i]==0) printf("%d ", i);

    return 0;

}


Main Source : Jane Alam Jan

int N = 5000;int status[5001];// status[i] = 0, if i is prime
// status[i] = 1, if i is not a prime
int main() {int i, j;// initially we think that all are primesfor( i = 2; i <= N; i++ )
status[i] = 0;
for( i = 3; i <= N; i += 2 ) {if( status[i] == 0 ) {// so, i is a prime, so, discard all the multiples
// 3 * i is odd, since i is odd. And j += 2 * i, so, the next odd
// number which is multiple of i will be found
for( j = 3 * i; j <= N; j += 2 * i )
status[j] = 1;
// status of the multiple is 1}
}
// print the primesprintf("2 ");for( i = 3; i <= N; i += 2 ) {if( status[i] == 0 ) {// so, i is primeprintf("%d ", i);
}
}
return 0;
}
  


No comments:

Post a Comment

Speedup Android Studio

Go to Help ->Edit Custom VM Options, and chnge this 4 setting. After that close your Android Studio. This settings are for 8gb of ram pc...