-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fibonacci.java
77 lines (65 loc) · 1.36 KB
/
Fibonacci.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import static org.junit.Assert.*;
import org.junit.Test;
/**
* The following table shows the first ten numbers in the
* Fibonacci sequence:
*
* n : 0 1 2 3 4 5 6 7 8 9 ...
* Fib(n) : 1 1 2 3 5 8 13 21 34 55 ...
*
* @author <put your name here>
*/
public class Fibonacci {
/**
* TODO: Implement the recursive definition directly.
*/
public static int fib1(int n) {
if (n<2) {
return 1;
}
return fib1(n - 1) + fib1(n - 2);
}
/**
* TODO: Implement recursively by calling a tail-recursive helper.
*/
public static int fib2(int n){
public int helper(int a, int b, int n):
if (n<2){
return 1;
}else{
return fib_help(b, a+b, n-1);
}
return helper(0, 1, n)
}
/**
* TODO: Run this class as an application.
*/
public static void main(String... args) {
assert fib1(9) == 55;
assert fib2(9) == 55;
}
/**
* TODO: Run this class as a JUnit test. Add additional tests to
* the following methods.
*/
@Test
public void testFib1() {
assertEquals(55, fib1(9));
}
@Test
public void testFib2() {
assertEquals(55, fib2(9));
}
@Test
public void testFib2() {
assertEquals(0, fib2(0));
}
@Test
public void testFib2() {
assertEquals(1, fib2(1));
}
@Test
public void testFib2() {
assertEquals(2, fib2(2));
}
}