-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathProblem2.java
More file actions
34 lines (29 loc) · 883 Bytes
/
Problem2.java
File metadata and controls
34 lines (29 loc) · 883 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
package io.zipcoder;
public class Problem2 {
int numPrevious = 0;
int numPrePrevious = 1;
StringBuilder fibonacciString = new StringBuilder();
public String fibonacciIteration(int n) {
if (n == 0) {
fibonacciString.append("0");
}
if (n == 1) {
fibonacciString.append("0, 1");
} else {
fibonacciString.append("0, 1");
for (int nextNum = 0; nextNum < n; nextNum++) {
nextNum = numPrevious + numPrePrevious;
if (nextNum > n) {
break;
}
fibonacciString.append(", " + nextNum);
numPrevious = numPrePrevious;
numPrePrevious = nextNum;
}
}
return fibonacciString.toString();
}
// public String fibonacciRecursion(int n) {
//
// }
}