Skip to content

Instantly share code, notes, and snippets.

@nichtemna
Created August 10, 2016 16:10
Show Gist options
  • Save nichtemna/8b1abb31e4d2d987a5a38026af2d66de to your computer and use it in GitHub Desktop.
Save nichtemna/8b1abb31e4d2d987a5a38026af2d66de to your computer and use it in GitHub Desktop.
Fibonacci number by table method
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