long long int findPrimeSum(long long int limit) { long long int i,j; long long int sum=2;//Add first prime addPrime(2); bool isPrime; for(i=3; i<limit; i++) { isPrime=true; for(j=0; j<numPrimes; j++) { if(i%primes[j]==0) { isPrime=false; break; } } if(isPrime==true) { addPrime(i); sum+=i; } } return sum; }
int main(){ int candidate=3, indx=0, indxLast=1, size=1, limit=1; int *primes=(int*)malloc(sizeof(int)); *primes=2; // Get upper limit from user while(1>=limit){ printf("Up to which number would you like to know all the prime numbers? "); scanf("%d", &limit); if(1>=limit){ printf("Don't be silly...\n"); } } // Search for primes below limit printf("{%d", *primes); while(limit>=candidate){ if(0==candidate%(*(primes+indx))){ candidate+=2; indx=0; } else if(0!=candidate%(*(primes+indx)) && sqrt(candidate)>=*(primes+indx)){ ++indx; } else{ addPrime(&primes, &size, indxLast, candidate); printf(", %d", *(primes+indxLast)); candidate+=2; ++indxLast; indx=0; } } printf("}\n"); }
uint64_t isPrime(const uint64_t n, uint64_t ** const primes, uint64_t * const count, uint64_t * const size) { uint64_t i,prime,bound; for(i=0,prime=1,bound=(uint64_t)sqrt(n)+1; prime && i<*count && (*primes)[i]<=bound; ++i) prime = n%(*primes)[i]; if(prime) addPrime(n,primes,count,size); return prime; }