Skip to content

Instantly share code, notes, and snippets.

@kawakami-o3
Created July 22, 2012 06:48
Show Gist options
  • Save kawakami-o3/3158725 to your computer and use it in GitHub Desktop.
Save kawakami-o3/3158725 to your computer and use it in GitHub Desktop.
Fibonacci in Java
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