Skip to content

Instantly share code, notes, and snippets.

@wallstop
Created September 10, 2013 02:43
Show Gist options
  • Save wallstop/6504372 to your computer and use it in GitHub Desktop.
Save wallstop/6504372 to your computer and use it in GitHub Desktop.
import java.util.HashSet;
public class main
{
public static void main(String [] args)
{
fib(10);
}
// Misses out on the first 1 :/
static HashSet<Integer> cache = new HashSet<Integer>();
public static int fib(int n)
{
int retVal = 0;
if(n == 0 || n == 1)
retVal = 1;
else
retVal = fib(n - 1) + fib(n - 2);
if(cache.add(retVal))
System.out.println(retVal);
return retVal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment