Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Created April 17, 2017 13:28
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 LuxXx/fe1804e0dc89e1efbba7b49e89918a68 to your computer and use it in GitHub Desktop.
Save LuxXx/fe1804e0dc89e1efbba7b49e89918a68 to your computer and use it in GitHub Desktop.
Project Euler - Problem 25
package euler;
import java.math.BigInteger;
import java.util.HashMap;
public class Fib {
private static HashMap<BigInteger, BigInteger> map = new HashMap<>();
public static void main(String[] args) {
for (int i = 1; i < Integer.MAX_VALUE; i++) {
if (f(BigInteger.valueOf(i)).toString().length() == 1000) {
System.out.println(i);
break;
}
}
}
public static BigInteger f(BigInteger n) {
if (map.containsKey(n)) return map.get(n);
if (n.equals(BigInteger.ONE)) return BigInteger.ONE;
else if (n.equals(BigInteger.valueOf(2))) return BigInteger.ONE;
else {
BigInteger i = f(n.subtract(BigInteger.ONE)).add(f(n.subtract(BigInteger.valueOf(2))));
map.put(n, i);
return i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment