-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler2.java
More file actions
53 lines (44 loc) · 738 Bytes
/
euler2.java
File metadata and controls
53 lines (44 loc) · 738 Bytes
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
By considering the terms in the Fibonacci sequence whose
values do not exceed four million, find the sum of the even-
valued terms.
*/
class Euler2
{
public static void main(String argz[])
{
int termNo = 1;
int fibVal = 1;
int sum = 0;
while(fibVal < 4000000)
{
if(fibVal%2 == 0)
{
sum += fibVal;
}
fibVal = fibonacciGenerator(++termNo);
}
System.out.println(sum);
}
static int fibonacciGenerator(int termNo)
{
if(termNo == 1)
{
return 1;
}
int first = 1;
int second = 1;
for(int i = termNo; i>1; i--)
{
if (first > second)
{
second = first + second;
}
else
{
first = first + second;
}
}
return first > second ? first : second;
}
}