Skip to content

Commit 04a46c3

Browse files
committed
Add naive calculation of Fibonacci numbers
1 parent 2bd7916 commit 04a46c3

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy