I have this assignment to write the optimized space and time code for finding the sum of all primes under 2 million. I'm using the following function to check if each number is prime:
int IsPrime(unsigned int number) {
if (number <= 1) return 0;
unsigned int i;
for (i=2; i*i<=number; i++) {
if (number % i == 0) return 0;
}
return 1;
}
And running a for
loop from 1 to 2 million using a long double
, and checking each number if it is prime and then adding it. Is there a more optimized method to do this?
i < sqrt(number)
, wheresqrt(number)
is a constant for the function. \$\endgroup\$