Created
September 10, 2013 02:43
-
-
Save wallstop/6504372 to your computer and use it in GitHub Desktop.
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.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