Created
August 10, 2016 16:10
-
-
Save nichtemna/8b1abb31e4d2d987a5a38026af2d66de to your computer and use it in GitHub Desktop.
Fibonacci number by table method
This file contains hidden or 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.Scanner; | |
public class Fib { | |
public static void main(String[] args) { | |
Scanner scaner = new Scanner(System.in); | |
int n = scaner.nextInt(); | |
if (n <= 80) { | |
System.out.println("Fibonacci number for " + n + " = " + getFibonacci(n)); | |
} else { | |
System.out.println("To big input"); | |
} | |
} | |
private static long getFibonacci(int n) { | |
long[] fib = new long[n + 1]; | |
for (int i = 0; i < fib.length; i++) { | |
if (i <= 1) { | |
fib[i] = i; | |
} else { | |
fib[i] = fib[i - 1] + fib[i - 2]; | |
} | |
} | |
int pos = 0; | |
for (long i : fib) { | |
System.out.println(pos + " " + i); | |
pos++; | |
} | |
return fib[n]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment