Created
July 22, 2012 06:48
-
-
Save kawakami-o3/3158725 to your computer and use it in GitHub Desktop.
Fibonacci in Java
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.util.*; | |
public class Fib { | |
Map<Integer,Long> memo; | |
public Fib() { | |
memo = new HashMap<Integer,Long>(); | |
memo.put(0,0L); | |
memo.put(1,1L); | |
} | |
public Long get(int n) { | |
if (memo.get(n) != null) { | |
return memo.get(n); | |
} else { | |
Long result = get(n-1) + get(n-2); | |
memo.put(n,result); | |
return result; | |
} | |
} | |
public static void main(String[] args) { | |
for (int i=0 ; i<50 ; i++) { | |
System.out.println(new Fib().get(i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment