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
24 changes: 24 additions & 0 deletions src/main/java/io/zipcoder/Problem2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
package io.zipcoder;

public class Problem2 {

public String iterationMethod(int n){
if(n <= 1) {
return String.valueOf(n);
}
int num = 1;
int prevNum = 1;

for(int i=2; i<n; i++) {
int temp = num;
num += prevNum;
prevNum = temp;
}
return String.valueOf(num);
}

public String recursionMethod(int n){
if(n == 0)
return String.valueOf(0);
else if(n == 1)
return String.valueOf(1);
else
return recursionMethod(n - 1) + recursionMethod(n - 2);
}
}
20 changes: 20 additions & 0 deletions src/test/java/io/zipcoder/Problem2Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
package io.zipcoder;

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

public class Problem2Test {

Problem2 problem2 = new Problem2();

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

}

@Test
public void recursionMethodTest() throws Exception {
String expected = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144";
String actual = problem2.recursionMethod(150);
Assert.assertEquals(expected,actual);
}
}