-
Notifications
You must be signed in to change notification settings - Fork 20.5k
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
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ee65f7
Added Resursive implementation to get nth fibonacci number with memoi…
pri-sin 31bb829
Added Resursive implementation to get nth fibonacci number with memoi…
pri-sin 953f9d8
Update src/main/java/com/thealgorithms/maths/FibonacciJavaRecursion.java
pri-sin f25c8d4
Added Resursive implementation to get nth fibonacci number with memoi…
pri-sin 8d484f8
Removed conflicts
pri-sin 9dffc9c
Merge branch 'master' into priyas-branch
pri-sin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/com/thealgorithms/maths/FibonacciJavaRecursion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import java.math.BigInteger; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* author : @pri-sin | ||
* This class provides methods for calculating Fibonacci numbers using Recursion with Memoization. | ||
*/ | ||
public final class FibonacciJavaRecursion { | ||
static Map<Integer, BigInteger> fibonacciMap=new HashMap<>(); | ||
|
||
private FibonacciJavaRecursion() { | ||
// Private constructor to prevent instantiation of this utility class. | ||
pri-sin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* 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) { | ||
fibonacciMap.put(n, BigInteger.valueOf(n)); | ||
return BigInteger.valueOf(n); | ||
} | ||
pri-sin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if(!fibonacciMap.containsKey(n)) { | ||
fibonacciMap.put(n, computeRecursively(n-2).add(computeRecursively(n-1))); | ||
} | ||
|
||
return fibonacciMap.get(n); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/thealgorithms/maths/FibonacciJavaRecursionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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; | ||
|
||
public class FibonacciJavaRecursionTest { | ||
@Test | ||
public void checkValueAtZero() { | ||
assertEquals(BigInteger.ZERO, FibonacciJavaRecursion.computeRecursively(0)); | ||
} | ||
|
||
@Test | ||
public void checkValueAtOne() { | ||
assertEquals(BigInteger.ONE, FibonacciJavaRecursion.computeRecursively(1)); | ||
} | ||
|
||
@Test | ||
public void checkValueAtTwo() { | ||
assertEquals(BigInteger.ONE, FibonacciJavaRecursion.computeRecursively(2)); | ||
} | ||
pri-sin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
@Test | ||
public void checkRecurrenceRelation() { | ||
for (int i = 0; i < 100; ++i) { | ||
assertEquals(FibonacciJavaRecursion.computeRecursively(i + 2), FibonacciJavaRecursion.computeRecursively(i + 1).add(FibonacciJavaRecursion.computeRecursively(i))); | ||
} | ||
} | ||
|
||
@Test | ||
public void checkNegativeInput() { | ||
assertThrows(IllegalArgumentException.class, () -> { FibonacciJavaRecursion.computeRecursively(-1); }); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.