Saturday, November 26, 2016

Best Sieve Technique

#include<iostream>
#include<cstdio>
#include<cmath>

int N = 5000, status[2501];
int main() {
    int i, j, sqrtN;
    for( i = 2; i <= N >> 1; i++ ) status[i] = 0;
    sqrtN = int( sqrt((double)N) ); // have to check primes up to (sqrt(N))
    for( i = 3; i <= sqrtN; i += 2 ) {
        if( status[i>>1] == 0 ) {
// so, i is a prime, so, discard all the multiples
// j = i * i, because it’s the first number to be colored
            for( j = i * i; j <= N; j += i + i )
                status[j>>1] = 1; // status of the multiple is 1
        }
    }
// print the primes
    printf("2 ");
    for( i = 3; i <= N; i += 2 ) {
        if( status[i>>1] == 0 )
            printf("%d ", i);
    }


}

by: Jane Alam Jan

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;
}
  


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...