Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 27, 2018 18:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/2210823f41c8b236f4db to your computer and use it in GitHub Desktop.
Save thmain/2210823f41c8b236f4db to your computer and use it in GitHub Desktop.
public class Fibonacci{
public static int fibTopDown(int n, int [] fib) {
if(n==0) return 0;
if(n==1) return 1;
if(fib[n]!=0){
return fib[n];
}else{
fib[n] = fibTopDown(n-1, fib) + fibTopDown(n-2, fib);
return fib[n];
}
}
public static void main(String[] args){
int n = 10;
int [] fib = new int[n+1];
System.out.println(fibTopDown(n, fib));
}
}
@robertpaul01
Copy link

is this supposed to be 0?

Copy link

ghost commented May 30, 2017

if(n==0) return 1; Should return 0 (zero)

@thmain
Copy link
Author

thmain commented May 27, 2018

Thanks , updated it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment