completed even fibonacci numbers
This commit is contained in:
parent
8e77b319b3
commit
96b7f93332
30
src/evenfibonaccinumbers/Fibonacci.java
Normal file
30
src/evenfibonaccinumbers/Fibonacci.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package evenfibonaccinumbers;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Fibonacci {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
int upperBound = 4000000;
|
||||||
|
|
||||||
|
int total = 2;
|
||||||
|
|
||||||
|
List<Integer> fibonacci = new ArrayList<Integer>();
|
||||||
|
|
||||||
|
fibonacci.add(1);
|
||||||
|
fibonacci.add(2);
|
||||||
|
|
||||||
|
int next;
|
||||||
|
while(fibonacci.get(fibonacci.size() - 1) < upperBound) {
|
||||||
|
next = fibonacci.get(fibonacci.size() - 2) + fibonacci.get(fibonacci.size() - 1);
|
||||||
|
fibonacci.add(next);
|
||||||
|
if(next % 2 == 0)
|
||||||
|
total += next;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("The total of even fibonacci numbers below " + upperBound + " is " + total);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user