forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciLoopEx.java
More file actions
31 lines (20 loc) · 619 Bytes
/
FibonacciLoopEx.java
File metadata and controls
31 lines (20 loc) · 619 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
package com.zetcode;
import java.math.BigInteger;
public class FibonacciLoopEx {
public static BigInteger fibonacci(int n) {
if (n <= 1) return BigInteger.valueOf(n);
BigInteger previous = BigInteger.ZERO, next = BigInteger.ONE, sum;
for (int i = 2; i <= n; i++) {
sum = previous;
previous = next;
next = sum.add(previous);
}
return next;
}
public static void main(String[] args) {
for (int i = 0; i <= 99; i++) {
BigInteger val = fibonacci(i);
System.out.println(val);
}
}
}