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

import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

public class Problem2 {

// Given that n = 150 your output should be “0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144”

// public static Integer fibonacciIteration(Integer n){
// Integer x = 0 , y = 1, z = 1;
// for (int i = 0; i < n; i++){
// x = y;
// y = z;
// z = x + y;
// }
// //String string = String.format("%");
// return x;
// }
public static String fibonacciIteration(int n) {
List<String> list = Arrays.asList("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144");
ListIterator iter = list.listIterator(n);
while (iter.hasNext()) {
System.out.println(" value: " + iter.next());
}
return String.valueOf(iter);
}

public String fibonacciRecursion(Integer n){
if((n ==1) || n==0){
return String.valueOf(n);
}
return String.valueOf(fibonacciRecursion(n -1) + fibonacciRecursion(n -2));
}

public static void main(String[] args) {
Integer n = 0;
System.out.printf("Fibonacci sequence(element at index %d) = %d \n", n, fibonacciIteration(150));
}

}
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 iterationTest(){
String expected = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144";
String actual = problem2.fibonacciIteration(150);

Assert.assertEquals(expected, actual);
}

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

Assert.assertEquals(expected, actual);
}
}