-
Finding primes...
Code:
void determinePrimes(int maxPrime)
{
int numPrimes = 0, *primes = new int[1.01 * (maxPrime / (log(maxPrime) - 1))];
for (int j = 3; j <= maxPrime; j += 2)
if (isPrime(j, primes))
primes[++numPrimes] = j;
delete[] primes;
}
inline bool isPrime(int number, int *primes)
{
int biggestTest = sqrt(number) + 1;
for (int j = 0; primes[j] <= biggestTest; ++j)
if (number % primes[j] == 0) return 0;
return 1;
}
When I put in a number greater than around 100,000, the program performs an illegal operation and gets shut down. Is there any way I can allocate more memory for the array that seems to be causing the problem?
-
You sure it's not just overflowing the number of primes you allocated?
I don't know about the mathematical frequency of primes in a list of in a general continous sequence of N integers (I've actually thought about it a bit though).
It's possible, however, that there are more primes than
1.01 * (maxPrime / (log(maxPrime) - 1)).
Try making that 1.01 a 1.5 and seeing if that works.
-
Nope, that's not it...I tested it with a mathematical reference book. My algorithim allocates 78800 spaces in the array for 78498 primes, which is enough efficiency for me.
What else could be the problem?