Getting an integer n
, return the next digit after the nth digit of the fibonacci sequence.
For example:
n == 3, return 2.
n == 2, return 1.
n == 12, return 144.
Fib sequence. 0,1,1,2,3,5,8,13,21,34,55,89,144. . .
public class nthFib{
public static int fib(int n){
return n == 1 ? 1 : n == 0 ? 0 : (fib(n - 1) + fib(n - 2));
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class Tests {
public static int fibonacci(int n){
return n == 1 ? 1 : n == 0 ? 0 : (fibonacci(n - 1) + fibonacci(n - 2));
}
@Test
public void test1() {
for (int r=0; r<10; r++) {
int n = (int)(Math.random() * 20 + 1);
assertEquals(fibonacci(n), nthFib.fib(n));
}
}
@Test
public void test2() {
for (int r=0; r<10; r++) {
int n = (int)(Math.random() * 20 + 1);
assertEquals(fibonacci(n), nthFib.fib(n));
}
}
@Test
public void test3() {
for (int r=0; r<10; r++) {
int n = (int)(Math.random() * 20 + 1);
assertEquals(fibonacci(n), nthFib.fib(n));
}
}
@Test
public void test4() {
for (int r=0; r<10; r++) {
int n = (int)(Math.random() * 20 + 1);
assertEquals(fibonacci(n), nthFib.fib(n));
}
}
@Test
public void test5() {
for (int r=0; r<10; r++) {
int n = (int)(Math.random() * 20 + 1);
assertEquals(fibonacci(n), nthFib.fib(n));
}
}
@Test
public void test6() {
for (int r=0; r<10; r++) {
int n = (int)(Math.random() * 20 + 1);
assertEquals(fibonacci(n), nthFib.fib(n));
}
}
}