forked from anitaa1990/Android-Cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciSeries.java
More file actions
38 lines (28 loc) · 810 Bytes
/
FibonacciSeries.java
File metadata and controls
38 lines (28 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package dynamicprogramming;
import java.util.Arrays;
public class FibonacciSeries {
/*
* Find the nth fibonacci number.
* Input: 10
* Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
*
* Fibonacci series are the sum of the previous two numbers i.e. Fib(N): Fib(N-1) + Fib(N-2), for N >= 2
* */
public static int[] getFibonacciNumbers(int n) {
int one = 0;
int two = 1;
int[] result = new int[n];
result[0] = one;
result[1] = two;
for(int i=2; i<n; i++) {
result[i] = two + one;
one = two;
two = result[i];
}
return result;
}
public static void main(String[] args) {
int n = 10;
System.out.println(Arrays.toString(getFibonacciNumbers(n)));
}
}