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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "stdafx.h"
#include <iostream>
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.
after reading it:
the algorithm trades 40KB of memory space with several hundred thousand CPU operations.
i admit this algorithm is better than mine.

i'm hoping to see a yet better algorithm, oh i know it:
go to wolframalpha.com and type:
Prime[10001]
it will give you the answer in microseconds. :P
Topic archived. No new replies allowed.