36 lines
681 B
C
36 lines
681 B
C
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <math.h>
|
|
|
|
bool checkPrime(int p, int *pptr){
|
|
int sqrt_p = sqrt(p);
|
|
for(int i = 0; i < sqrt_p; i++){
|
|
if(p % *(pptr+i) == 0){return false;}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int main(){
|
|
double max = pow(10,7);
|
|
int pi_max = (int)(ceil(1.1*(max/log(max))));
|
|
int primes[pi_max];
|
|
primes[0] = 2;
|
|
int *pptr = &primes[0];
|
|
int count = 1;
|
|
|
|
//calculate primes
|
|
for(int c = 3; c <= max; c+=2){
|
|
if(checkPrime(c, pptr)){
|
|
primes[count] = c;
|
|
count++;
|
|
}
|
|
}
|
|
|
|
//print primes
|
|
printf("The set of all primes below %d is: ", (int)max);
|
|
for (int i = 0; i < count; i++){
|
|
printf("%d ", primes[i]);
|
|
}
|
|
return 0;
|
|
}
|