Skip to content

Instantly share code, notes, and snippets.

@bekce
Created March 22, 2017 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bekce/45c846e058ffef7020fca34afcf8e46f to your computer and use it in GitHub Desktop.
Save bekce/45c846e058ffef7020fca34afcf8e46f to your computer and use it in GitHub Desktop.
public static BigInteger fibonacci(long n) {
if (n < 1) throw new IllegalArgumentException("n>=1 must hold");
BigInteger cur = BigInteger.ONE, t1 = BigInteger.ONE, t2 = BigInteger.ONE;
for (long i = 2; i < n; i++) {
cur = t1.add(t2);
t1 = t2;
t2 = cur;
}
return cur;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment