Project Euler #2

I'm still getting message "wrong result".


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
36
37
38
#include <iostream>
using namespace std;

	


int main()


{

	int n0=0;
	int n2;
	int suma=0;
	
	
	
	 
	 for(int n1=1;n1<4000000;n1++)
	 {
	

	  n2 = n1;
       n1 = n0 + n1;
       n0 = n2;
	 
	 
	 
	 if(n1%2==0)
	 {suma=suma+n1 ;
	 
		 }
		 
		 }
		 	
	 cout<<suma;

}

What's wrong?
I think that n1++
at line 19 makes it wrong

and perhaps suma should be in long long
just in case it overflows

First iteration:
n1 = 1, n2 = 1, n1 = 1, n0 = 1
1 is odd

Second iteration:
n1 = 2, n2 = 2, n1 = 3, n0 = 2
3 is odd

Third iteration:
n1 = 4, n2 = 4, n1 = 6, n0 = 4
6 is even

Fourth iteration:
n1 = 7, n2 = 7, n1 = 11, n0 = 7
11 is odd

Is 1, 3, 6, 11, ... the serie that you are looking for?
Topic archived. No new replies allowed.