Skip to content

Added Recursive implementation to get nth fibonacci number with memoization #5497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
40 changes: 40 additions & 0 deletions src/main/java/com/thealgorithms/maths/FibonacciWithDP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.thealgorithms.maths;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* author : @pri-sin
* This class provides methods for calculating Fibonacci numbers using Recursion with Memoization.
*/
public final class FibonacciWithDP {
static List<BigInteger> fibonacciList=new ArrayList<>(Arrays.asList(BigInteger.ZERO,BigInteger.ONE));

private FibonacciWithDP() {
}

/**
* Calculates the nth Fibonacci number.
*
* @param n The index of the Fibonacci number to calculate.
* @return The nth Fibonacci number as a BigInteger.
* @throws IllegalArgumentException if the input 'n' is a negative integer.
*/
public static BigInteger computeRecursively(final int n) {
if (n < 0) {
throw new IllegalArgumentException("Input 'n' must be a non-negative integer.");
}

if (n <= 1) {
return fibonacciList.get(n);
}

if(fibonacciList.size()<=n) {
fibonacciList.add(computeRecursively(n-2).add(computeRecursively(n-1)));
}

return fibonacciList.get(n);
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/thealgorithms/maths/FibonacciWithDPTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.math.BigInteger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class FibonacciWithDPTest {
@Test
public void checkValueAtZero() {
assertEquals(BigInteger.ZERO, FibonacciWithDP.computeRecursively(0));
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
public void checkValueAtOneAndTwo(int number) {
assertEquals(BigInteger.ONE, FibonacciWithDP.computeRecursively(number));
}

@Test
public void checkRecurrenceRelation() {
for (int i = 0; i < 100; ++i) {
assertEquals(FibonacciWithDP.computeRecursively(i + 2), FibonacciWithDP.computeRecursively(i + 1).add(FibonacciWithDP.computeRecursively(i)));
}
}

@Test
public void checkNegativeInput() {
assertThrows(IllegalArgumentException.class, () -> { FibonacciWithDP.computeRecursively(-1); });
}
}