Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fibonacci 2
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
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)); } }
[
+
]
Show input
Compilation time: 0.62 sec, absolute running time: 0.14 sec, cpu time: 0.06 sec, memory peak: 18 Mb, absolute service time: 0,77 sec
edit mode
|
history
|
discussion
0 1 1 2 3 5 8 13 21 34