diff --git a/src/main/java/io/zipcoder/Problem2.java b/src/main/java/io/zipcoder/Problem2.java index e0af6a0..a88b4c8 100644 --- a/src/main/java/io/zipcoder/Problem2.java +++ b/src/main/java/io/zipcoder/Problem2.java @@ -1,4 +1,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) { +// +// } + +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/Problem2Test.java b/src/test/java/io/zipcoder/Problem2Test.java index fa9ffa5..4490a64 100644 --- a/src/test/java/io/zipcoder/Problem2Test.java +++ b/src/test/java/io/zipcoder/Problem2Test.java @@ -1,4 +1,21 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem2Test { + + @Test + public void fibonacciIterationTest() { + Problem2 problem2Test = new Problem2(); + String expected = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144"; + String actual = problem2Test.fibonacciIteration(150); + Assert.assertEquals(expected, actual); + } + + @Test + public void fibonacciRecursionTest() { + + } + }