Skip to content

Instantly share code, notes, and snippets.

@edalorzo
Created May 9, 2013 04:55
Show Gist options
  • Save edalorzo/5545630 to your computer and use it in GitHub Desktop.
Save edalorzo/5545630 to your computer and use it in GitHub Desktop.
Memoized Fibonacci Numbers with Java 8
static Map<Integer,Long> memo = new HashMap<>();
static {
memo.put(0,0L);
memo.put(1,1L);
}
public static long fibonacci(int x) {
return memo.computeIfAbsent(x, n -> Math.addExact(fibonacci(n-1), fibonacci(n-2)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment