Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
Fibonacci 2
import java.util.*; import java.lang.*; /*Taken from http://introcs.cs.princeton.edu/java/23recursion/TopDownFibonacci.java.html 20170504*/ class Rextester { private static long[] f = new long[92]; public static long fibonacci(int n) { if (n == 0) return 0; if (n == 1) return 1; // return cached value (if previously computed) if (f[n] > 0) return f[n]; // compute and cache value f[n] = fibonacci(n-1) + fibonacci(n-2); return f[n]; } public static void main(String[] args) { int n = 10; for (int i = 0; i <= n; i++) System.out.print(" i" + i + ": " + fibonacci(i)); } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Rextester.java
3d
Size and signedness of Java ints and longs
linked lists
vsecodertest2
extendd11
Java tree
생성자
jb6.6 str rec
Coding Challenge - 01 (Even numbers)
Please log in to post a comment.