anotherCha
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* FibonacciSEQUENSE
|
||||
*/
|
||||
public class FibonacciSEQUENSE {
|
||||
|
||||
public static long[] fibCache;
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 1 1 2 3 5 8 13
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.println("Please enter a number to get the corresponding Fibonacci Number: ");
|
||||
int x = sc.nextInt();
|
||||
fibCache = new long[1 + x];
|
||||
|
||||
for (int i = 0; i < x; i++) {
|
||||
System.out.println(fibSequence(i));
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
|
||||
public static long fibSequence(int n) {
|
||||
if (n <= 1) {
|
||||
return n;
|
||||
}
|
||||
if (fibCache[n] != 0) {
|
||||
return fibCache[n];
|
||||
}
|
||||
long fibNum = (fibSequence(n - 1) + fibSequence(n - 2));
|
||||
fibCache[n] = fibNum;
|
||||
return fibNum;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user