Created
April 17, 2017 13:28
-
-
Save LuxXx/fe1804e0dc89e1efbba7b49e89918a68 to your computer and use it in GitHub Desktop.
Project Euler - Problem 25
This file contains 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
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