-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvenFib.java
35 lines (30 loc) · 874 Bytes
/
EvenFib.java
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
//https://www.hackerrank.com/contests/projecteuler/challenges/euler002/problem?isFullScreen=true
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static Long evenFibSum(Long n) {
Long evenFib = 0L;
Long fib = 1L;
Long fib0 = 1L;
Long fib1 = 1L;
for(Long i = 2L; true; i++) {
fib = fib0+fib1;
fib0 = fib1;
fib1 = fib;
if(fib >= n) break;
if(fib%2 == 0) evenFib += fib;
}
return evenFib;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
long n = in.nextLong();
System.out.println(evenFibSum(n));
}
}
}