Skip to content

Commit 05a2a3d

Browse files
authored
Create FibonacciLoopEx.java
1 parent fd2f98f commit 05a2a3d

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

algorithms/FibonacciLoopEx.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.zetcode;
2+
3+
import java.math.BigInteger;
4+
5+
public class FibonacciLoopEx {
6+
7+
public static BigInteger fibonacci(int n) {
8+
9+
if (n <= 1) return BigInteger.valueOf(n);
10+
11+
BigInteger previous = BigInteger.ZERO, next = BigInteger.ONE, sum;
12+
13+
for (int i = 2; i <= n; i++) {
14+
15+
sum = previous;
16+
previous = next;
17+
next = sum.add(previous);
18+
}
19+
20+
return next;
21+
}
22+
23+
public static void main(String[] args) {
24+
25+
for (int i = 0; i <= 99; i++) {
26+
27+
BigInteger val = fibonacci(i);
28+
System.out.println(val);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)