Last active
March 31, 2019 09:24
-
-
Save jinahya/582560103afa3aa4c733a9e51ef53d48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.util.Arrays; | |
public class FibonacciNumber { | |
static final BigInteger F0 = BigInteger.ZERO; | |
static final BigInteger F1 = BigInteger.ONE; | |
static BigInteger f(final int n) { | |
if (n <= 0) return F0; | |
if (n == 1) return F1; | |
final BigInteger[] a = new BigInteger[] {F0, F1}; | |
for (int i = 2; i < n; i++) { | |
final BigInteger b = a[1].add(a[0]); | |
a[0] = a[1]; | |
a[1] = b; | |
} | |
return a[1].add(a[0]); | |
} | |
public static void main(final String... args) { | |
Arrays.stream(args) | |
.mapToInt(Integer::parseInt) | |
.filter(n -> n >= 0) | |
.forEach(n -> System.out.printf("f(%1$d): %2$d\n", n, f(n))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment