-
-
Save GSkoba/883ba5dd083925bc839fb8518db89b7e to your computer and use it in GitHub Desktop.
Fibonacci number
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
import java.math.BigInteger; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Fibo { | |
Map<Integer, BigInteger> map = new HashMap<>(); | |
private BigInteger calcFibo(int arg) throws IllegalAccessException { | |
if (arg < 1) throw new IllegalAccessException(); | |
if (arg <= 2) return BigInteger.ONE; | |
return fibo(arg - 1).add(fibo(arg - 2)); | |
} | |
private BigInteger fibo(int arg) throws IllegalAccessException { | |
BigInteger value = map.get(arg); | |
if (value == null) { | |
value = calcFibo(arg); | |
map.put(arg, value); | |
} | |
return value; | |
} | |
public static void main(String[] args) throws IllegalAccessException { | |
Fibo fibo = new Fibo(); | |
System.out.println(fibo.fibo(100)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment