-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathProblem2.java
More file actions
40 lines (32 loc) · 1.09 KB
/
Problem2.java
File metadata and controls
40 lines (32 loc) · 1.09 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
package io.zipcoder;
public class Problem2 {
public String fibonacciIteration(Integer n) {
StringBuilder output = new StringBuilder("0");
Integer previousFibNumber = 0;
Integer currentFibNumber = 1;
while (currentFibNumber < n) {
output.append(", ").append(currentFibNumber);
Integer previousTemp = currentFibNumber;
currentFibNumber = currentFibNumber + previousFibNumber;
previousFibNumber = previousTemp;
}
return output.toString();
}
public Integer fibonacciRecursionInt(Integer n, Integer currentFibNumber) {
if (currentFibNumber < n) {
if (currentFibNumber == 0) {
return 0;
}
else if (currentFibNumber == 1) {
return 1;
}
else {
return fibonacciRecursionInt(n, currentFibNumber+1) +
fibonacciRecursionInt(n, currentFibNumber+2);
}
}
else {
return fibonacciRecursionInt(n, currentFibNumber);
}
}
}