void SieveofEratosthenes(long int a, long int b){ long int i, store; long int *arr; int flag=0; arr= malloc(b*sizeof(long int)); for(i=0;i<b;i++) arr[i]=0; for(i=2; i<a;i++){ if(a%i==0){ flag=1; break; } } if(flag==0) store=a-1; else store=a; printf("%ld\n", store); for (i= store ; i<b; ++i) { if ( arr[i] == 0 ) { //(i+1) is prime, print it and mark its multiples printf("%ld\n", i+1); markMultiples(arr, i+1, b); } } }
void sieve() { bool arr[MAX]; memset(arr, 0, sizeof(arr)); for (int i=1; i<MAX; ++i) { if ( arr[i] == 0 ) { prime[pc++]=i+1; markMultiples(arr, i+1); } } }
// A function to print all prime numbers smaller than n void SieveOfEratosthenes(int n) { // There are no prime numbers smaller than 2 if (n >= 2) { // Create an array of size n and initialize all elements as 0 bool arr[n]; memset(arr, 0, sizeof(arr)); /* Following property is maintained in the below for loop arr[i] == 0 means i is prime arr[i] == 1 means i is not prime */ for (int i=2; i<n; ++i) { if ( arr[i] == 0 ) { // i is prime, print it and mark its multiples printf("%d ", i); markMultiples(arr, i, n); } } } }
// A function to print all prime numbers smaller than n void SieveOfEratosthenes(int n, FILE* fp) {int count=0; // There are no prime numbers smaller than 2 if (n >= 2) { // Create an array of size n and initialize all elements as 0 bool arr[n]; memset(arr, 0, sizeof(arr)); /* Following property is maintained in the below for loop arr[i] == 0 means i + 1 is prime arr[i] == 1 means i + 1 is not prime */ for (int i=1; i<n; ++i) { if ( arr[i] == 0 ) { //(i+1) is prime, print it and mark its multiples fprintf(fp,"%d, ", i+1); markMultiples(arr, i+1, n); count++; } } } fprintf(fp,"%d, ", count);}