Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/main/java/io/zipcoder/Problem2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
package io.zipcoder;

public class Problem2 {

Problem2() {
}

public String iterationSolution(int n) {
StringBuilder solution = new StringBuilder();

int x = 0, y = 1, z = 1;

for (int i = 0; x < n; i++) {
solution.append(x + ", ");
x = y;
y = z;
z = x + y;
}

return solution.toString();
}


//Does not work, have to convert to last addition to solutionString to Integer and append I guess
private static String recursionSolution(int n, Integer x, String solution) {
String solutionString = solution + ", ";
String nResult;
if (n == 1)
return solutionString = "1";
else
solution = n * recursionSolution(n-1,solutionString);
return solutionString;
return null;
}
}
21 changes: 21 additions & 0 deletions src/test/java/io/zipcoder/Problem2Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Test;

public class Problem2Test {

Problem2 problem2 = new Problem2();

@Test
public void iterationSolution(){
String expected = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144";
String actual = problem2.iterationSolution(150);

Assert.assertEquals(expected,actual);
}

@Test
public void recursiveSolutionTest(){
String expected = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144";
String actual = problem2.iterationSolution(150);

Assert.assertEquals(expected,actual);
}
}