-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupGradCarMainProject.java
More file actions
61 lines (51 loc) · 1.62 KB
/
Copy pathupGradCarMainProject.java
File metadata and controls
61 lines (51 loc) · 1.62 KB
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
61
import java.util.ArrayList;
import java.util.List;
public class upGradCarMainProject {
public static void main(String[] args) {
System.out.println(recursive_factorial(8));
System.out.println(recursive_fibonacci(5));
System.out.println(factorialIterative(5));
}
public static int recursive_factorial(int number){
if(number == 0){
return 1;
}
if(number == 1){
return 1;
}
if(number == 2){
return 2;
}
return number * recursive_factorial(number - 1);
}
public static int factorialIterative(int number){
int newValu = 1;
if(number == 0)
return 1;
if(number == 1)
return 1;
if(number == 2)
return 2;
for(int i = 2; i <= number;i++) {
newValu = newValu * i;
}
return newValu;
}
// Given a number N return the index value of the Fibonacci sequence, where the sequence is:
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...
// the pattern of the sequence is that each value is the sum of the 2 previous values, that means that for N=5 → 2+3
public static int recursive_fibonacci(int num){
if(num < 2)
return num;
return recursive_fibonacci(num - 1) + recursive_factorial(num - 2);
}
public static int fibonacciIterative(int num){
ArrayList<Integer> arrVal = new ArrayList<Integer>();
arrVal.add(0,0);
arrVal.add(1,1);
for(int i = 2 ; i < num + 1; i++){
//arrVal.add(3,arrVal[i - 2] + arrVal[i - 1]);
}
return 0;
}
}