We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fd2f98f commit 05a2a3dCopy full SHA for 05a2a3d
1 file changed
algorithms/FibonacciLoopEx.java
@@ -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