Can anyone make any suggestions to make this code more efficient so that it doesn't cause a stack overflow? I think it's my humongous array, but I don't think I can change that without drastically reducing speed.
You are really over thinking this. You only need to keep the sum of the primes, you dont need the prime numbers themselves. I'll try to give you some pseudo code so not to ruin it for you.
1 2 3 4
loop from 3 to 2000000
is this number prime?
add to sum
output sum
The loop can be optimized a bit, but get a working version first.
Edit: fixed some typos.
I don't want to have to make a whole bunch of tests for this if I can just clear all the prime numbers. A number cannot be made without a prime number unless the number itself is prime, so I want to only check prime numbers to see if the number is prime. Having to check a whole bunch of numbers that will result in the same as any prime takes a lot more time than necessary, That's why I want to have that array for keeping the primes.
so I want to only check prime numbers to see if the number is prime
I have no idea what you mean here. If you mean only checking numbers with the properties all primes share (only thing I can think of is all primes are odd).
I don't understand that code. Now I completely revamped my code and got the same answer in significantly less time, but I still can't get the right answer for some reason.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
//declare variables
int result = 2;
int root;
for(int x = 3; x < 2000000; x += 2)
{
root = sqrt(double(x));
bool prime = true;
for(int y = 2; y <= root; y++)
{
if(x % y == 0)
{
prime = false;
break;
}//end if
}//end for
if(prime)
result += x;
}//end for
cout << result << endl;
return 0;
}
There's something seriously wrong with variable result. When you fix that, you'll get the correct answer (I know, because I just did). Happy logical thinking ^^
The correct answer is 142913828922, which requires at least 38 bits to be exactly represented. Your implementation likely uses 32-bit integers, so you're causing an overflow when trying to calculate such a large number.
Try using long long.
PS: Your loop over y can be made twice as fast this way: for(int y = 3; y <= root; y+=2)
I had the same exact issue, but I created a vector to store all of my prime numbers. It runs fairly quickly, a few seconds, on my desktop. It took me posting here as well to figure out what my issue was as well. I never would have imagined something that seemed so simple would overflow.
I want to only check prime numbers to see if the number is prime.
Your array is over sized.
To know all the primes that are less that 100 you only need the primes that are less than 10. (it applies to the sieve too)
Besides that, there are approx log(n) primes that are less than n.
I wasn't thinking when I wrote that. I meant to break if it was larger than the square root of y. Anyways, I eventually tried fstream and got way more than necessary. The file is 1.19 MB and the program takes about 15 minutes. The code above took seconds.
Thanks for all your help everybody. If you want to help on my new problem (Problem 13) I'm having issues with the result.