File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
main/java/com/anverbogatov/algorithms/calculations
test/java/com/anverbogatov/algorithms/calculations Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .anverbogatov .algorithms .calculations ;
2
+
3
+ public final class FibonacciNumbers {
4
+
5
+ /**
6
+ * Naive implementation of Fibonnacci numbers calculation.
7
+ * Recursion-based approach.
8
+ * Complexity is O(2^n).
9
+ *
10
+ * @param n - starting number
11
+ * @return sum of Fibonnaci numbers
12
+ */
13
+ public static int calculate (int n ) {
14
+ if (n == 0 ) {
15
+ return 0 ;
16
+ }
17
+ if (n == 1 ) {
18
+ return 1 ;
19
+ }
20
+ return calculate (n - 1 ) + calculate (n - 2 );
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ package com .anverbogatov .algorithms .calculations ;
2
+
3
+ import junit .framework .TestCase ;
4
+ import org .junit .Assert ;
5
+
6
+ public class FibonacciNumbersTest extends TestCase {
7
+
8
+ public void testCalculateForZero () {
9
+ Assert .assertEquals (0 , FibonacciNumbers .calculate (0 ));
10
+ }
11
+
12
+ public void testCalculateForOne () {
13
+ Assert .assertEquals (1 , FibonacciNumbers .calculate (1 ));
14
+ }
15
+
16
+ public void testCalculate () {
17
+ Assert .assertEquals (3524578 , FibonacciNumbers .calculate (33 ));
18
+ }
19
+ }
You can’t perform that action at this time.
0 commit comments