Project Euler problem 7 -> what is wrong with my code?

Hi there, I recently attempted to complete Project Euler problem number 7 : http://projecteuler.net/problem=7
For some reason, my code in C++ is unable to provide me with the correct result, whereas when I repeated the same logic in VB, I was able to get the correct answer.
If somebody could have a quick look through my code and try to spot what I may have done wrong, I would be very grateful;
thanks!

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include "stdafx.h" #include using namespace std; int main() { bool isPrime = true; int primes[10000]; int nextNumber = 3; primes[0] = 2; int position = 0; do { isPrime = true; for (int i = 0; i <=position; i++) { if (nextNumber % primes[i] == 0) { isPrime = false; cout << nextNumber << "div" << primes[i] << endl; } } if (isPrime = true) { position++; primes[position] = nextNumber; } nextNumber += 2; } while (position != 10000); cout << primes[9999] << endl; cout << primes[10000] << " is the answer" << endl; cin.clear(); cin.ignore(255, '\n'); cin.get(); return 0; }``````
On line 21 you are using = instead of ==.
I suspect it might have to do with line 21 and the difference between = and ==.

Also, line 30 accesses an element of primes that doesn't exist. If you want room to store 10001 elements, the array must have a size of 10001. (You don't calculate 10001 primes -- you're one short.)
Last edited on
can i ask why you store primes in the array, with a simple calculation you can see that it takes 40KB of memory for a simple task, where you don't need to use any of them nor to know it's location.
i don't think you're doing a pre-computation, are you?
if not, then you don't need this array.
@Rechard3, if OP wants an array to store his primes then by god he can use an array :)

As you said, it's only 40KB. It's not 1970 anymore ;)
 can i ask why you store primes in the array

Did you read the code? It would become obvious if you did. See, particularly, the loop beginning on line 15 and ending on line 20.
@ResidentBiscuit
 if OP wants an array to store his primes then by god he can use an array :)

haha, very funny.
a good programmer shall always look for the best algorithm possible, if you can save 40KB, that's good.
 As you said, it's only 40KB. It's not 1970 anymore ;)

if you go around wasting kilobytes for no good reason your program might easily reach several hundred megabytes where it could be just a few kilobytes big.
i thank you for your friendliness in talking, it's delightful.

@cire
you are right, i didn't read all the code, and this is my bad.