-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathFibonacci.java
60 lines (47 loc) · 973 Bytes
/
Fibonacci.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
System.out.println("Enter a number:");
Scanner in = new Scanner(System.in);
int result = fib(in.nextInt()); // first solution in use -> Main Solution
System.out.println(result);
in.close();
}
/* Main Solution */
static int fib(int n) {
if (n < 2) {
return n;
}
int a = 0;
int b = 1;
for (int i = 2; i <= n; i++) {
int temp = a + b;
a = b;
b = temp;
}
return b;
}
/* Recursive Solution */
static int fib2(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
return fib(n-1) + fib(n-2);
}
/* Dinamic Programming
* If needed all the elements in the sequence -> return the array
*/
static int fib3(int n) {
if (n < 2) {
return n;
}
int[] fibN = new int[n+1];
fibN[0] = 0;
fibN[1] = 1;
for (int i = 2; i <= n; i++) {
fibN[i] = fibN[i-1] + fibN[i-2];
}
return fibN[n];
}
}