Sieve of Eratosthenes

Hello there. Im working on a euler project problem. I'm attempting to use the "sieve of eratosthenes" (on wikipedia) pseudo code. Here's my interpretation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//euler prob_10
#include <iostream>
using namespace std;
//answer should be: 142913828922
int main(){
	int i,j,sum=0,n=2000000;
	bool list[n];
	list[0]=false;
	list[1]=false;
	for(i=2;i<n;i++)
		list[i]=true;
		
	for(i=2;i<n/2;i++){
		if(list[i]==true)
			for(j=2*i;j<=n;j+=i)
				list[j] = false;
	}
	for(i=2;i<=n;i++){
		if(list[i]==true)
			sum+=i;
	}
	cout<<sum<<endl;
	return 0;
}

when i run a smaller max range like 10 in for n, I get the correct answer. But not with 2000000. Any help? Thanks in advance!!
Last edited on
bool list[n]; This shouldn't work; you need to declare array sizes with a constant.
updated:
1
2
3
int i,j,sum=0;
	const int n=2000000;
	bool list[n];

no improvement
Topic archived. No new replies allowed.