-
Notifications
You must be signed in to change notification settings - Fork 20k
Added [FEATURE REQUEST] Golden Ration formula to find Nth Fibonacci number #4505 #4513
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
Merged
Merged
Changes from 133 commits
Commits
Show all changes
142 commits
Select commit
Hold shift + click to select a range
990737f
Create FibonacciNumber.java
debnath003 986de53
Update FibonacciNumber.java
debnath003 f0cb961
Update FibonacciNumber.java
debnath003 f10ba98
Merge branch 'master' into master
debnath003 458a67a
Merge branch 'master' into master
debnath003 af5ead3
Merge branch 'master' into master
debnath003 85aea40
Merge branch 'master' into master
debnath003 10b4020
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 9b591dc
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 83b6fd0
Update FibonacciNumber.java
debnath003 5fd14ae
Merge branch 'master' into master
debnath003 52d5d48
Update FibonacciNumber.java
debnath003 d923df5
Update FibonacciNumber.java
debnath003 d95ff92
Update FibonacciNumber.java
debnath003 d79c527
Create FibonacciNumberTest.java
debnath003 e03c75e
Update FibonacciNumberTest.java
debnath003 838cd9e
Merge branch 'master' into master
debnath003 0c12169
Update FibonacciNumberTest.java
debnath003 b8fb3e3
Update FibonacciNumberTest.java
debnath003 c8fe427
Update FibonacciNumberTest.java
debnath003 61c8ae8
Update FibonacciNumberTest.java
debnath003 3597f5e
Update FibonacciNumberTest.java
debnath003 9dc18ba
Update FibonacciNumberTest.java
debnath003 cc58ea7
Update FibonacciNumberTest.java
debnath003 03e1c1b
Update FibonacciNumberTest.java
debnath003 673bb54
Update FibonacciNumberTest.java
debnath003 11acae5
Update FibonacciNumberTest.java
debnath003 01834f9
Update FibonacciNumberTest.java
debnath003 494a5de
Update FibonacciNumberTest.java
debnath003 ba3a9b1
Update FibonacciNumberTest.java
debnath003 01a33ff
Update FibonacciNumberTest.java
debnath003 4d6df56
Update FibonacciNumberTest.java
debnath003 837f306
Update FibonacciNumber.java
debnath003 25e22bf
Update FibonacciNumberTest.java
debnath003 565a687
Update FibonacciNumberTest.java
debnath003 aaceb6f
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 253cda0
Update FibonacciNumber.java
debnath003 aa0265e
Update FibonacciNumberTest.java
debnath003 37cf8ee
Update FibonacciNumberTest.java
debnath003 eb52e31
Update FibonacciNumber.java
debnath003 21684e6
Update FibonacciNumberTest.java
debnath003 e9a5780
Merge branch 'master' into master
debnath003 75e31ed
Update FibonacciNumberTest.java
debnath003 2bff558
Delete src/main/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 dd6078c
Create FibonacciNumberTest.java
debnath003 9bb2b54
Update FibonacciNumber.java
debnath003 ff21124
Update FibonacciNumberTest.java
debnath003 86f83ab
Update FibonacciNumber.java
debnath003 7c85b73
Update FibonacciNumber.java
debnath003 f17dc7b
Merge branch 'master' into master
debnath003 e580f0e
Update FibonacciNumber.java
debnath003 151651f
Update FibonacciNumber.java
debnath003 bf53e85
Update FibonacciNumberTest.java
debnath003 45a64db
Merge branch 'master' into master
debnath003 529d9c5
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 ab9942e
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 e8000d6
Create FibonacciCalculator.java
debnath003 556aa8f
Update FibonacciNumberTest.java
debnath003 e34a19b
Update and rename FibonacciCalculator.java to FibCalc.java
debnath003 1a8fef2
Update FibonacciNumberTest.java
debnath003 56465dc
Update FibCalc.java
debnath003 361fdc9
Update FibonacciNumber.java
debnath003 2547ada
Merge branch 'master' into master
debnath003 3cf874b
Delete src/test/java/com/thealgorithms/maths/FibCalc.java
debnath003 bcae36f
Create FibCalc.java
debnath003 bbb3a6a
Update FibonacciNumberTest.java
debnath003 5fb25f4
Update FibCalc.java
debnath003 6fd2d76
Update FibonacciNumberTest.java
debnath003 2805d43
Update FibonacciNumber.java
debnath003 7fe6a1e
Update FibonacciNumberTest.java
debnath003 b666be8
Update FibonacciNumber.java
debnath003 be514bf
Update FibonacciNumber.java
debnath003 d85c849
Update FibonacciNumber.java
debnath003 960f0c0
Update FibonacciNumber.java
debnath003 6600421
Update FibonacciNumberTest.java
debnath003 c005dda
Update FibonacciNumber.java
debnath003 a77bdb3
Merge branch 'master' into master
debnath003 dbd1438
fix: use proper name
vil02 35e7f93
fix: use proper class name
vil02 34602ed
tests: add `returnsCorrectValues`
vil02 339c9a9
Update and rename FibCalc.java to Fibonacci.java
debnath003 66607ff
Update Fibonacci.java
debnath003 8969263
Update FibonacciNumber.java
debnath003 3bc67d5
Update FibonacciNumberTest.java
debnath003 049a818
Update FibonacciNumberTest.java
debnath003 ab6b1c3
Update Fibonacci.java
debnath003 859e0bd
Update FibonacciNumber.java
debnath003 5460411
Update and rename FibCalcTest.java to FibonacciTest.java
debnath003 b430ff4
Update FibonacciNumber.java
debnath003 89642b1
Update Fibonacci.java
debnath003 4f0b0f4
Update Fibonacci.java
debnath003 d2c738f
Update Fibonacci.java
debnath003 8a9b65c
Update FibonacciTest.java
debnath003 53545a6
Update Fibonacci.java
debnath003 62cd87c
Update src/main/java/com/thealgorithms/maths/Fibonacci.java
debnath003 227e08c
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 7fc9e74
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 e629bb3
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 ed272c4
Merge branch 'master' into master
debnath003 766b40c
Update FibonacciTest.java
debnath003 a328240
Update FibonacciNumberTest.java
debnath003 e7367b5
Update FibonacciNumberTest.java
debnath003 106e5b4
Update FibonacciTest.java
debnath003 8789440
Update src/main/java/com/thealgorithms/maths/Fibonacci.java
debnath003 ae09b5f
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 bc0112b
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 567606a
Update src/test/java/com/thealgorithms/maths/FibonacciTest.java
debnath003 191970b
Update src/main/java/com/thealgorithms/maths/Fibonacci.java
debnath003 7bc0b10
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 8bc8465
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 5581ea9
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 b223196
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 b9ac542
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 ce069f2
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 a943471
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 fd03500
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 a09b2c3
Update FibonacciNumber.java
debnath003 324086a
Update FibonacciNumber.java
debnath003 77f4635
Update Fibonacci.java
debnath003 044a9c4
Update FibonacciNumber.java
debnath003 04539a0
Update and rename FibonacciNumber.java to FibonacciNumberGoldenRation…
debnath003 f054ad6
Update and rename FibonacciNumberTest.java to FibonacciNumberGoldenRa…
debnath003 f348278
Update Fibonacci.java
debnath003 466c4d5
Update FibonacciNumberGoldenRation.java
debnath003 440da96
Update FibonacciNumberGoldenRationTest.java
debnath003 f722307
Update FibonacciTest.java
debnath003 6f43ecb
Update Fibonacci.java
debnath003 7985170
Update FibonacciNumberGoldenRationTest.java
debnath003 902ba1a
Update FibonacciNumberGoldenRationTest.java
debnath003 c356a1d
Update FibonacciNumberGoldenRation.java
debnath003 a0af070
Update FibonacciNumberGoldenRation.java
debnath003 e6d1bd9
Update FibonacciNumberGoldenRationTest.java
debnath003 e6da345
Update FibonacciNumberGoldenRationTest.java
debnath003 faca3e1
Update src/main/java/com/thealgorithms/maths/FibonacciNumberGoldenRat…
debnath003 6d4f92b
Update and rename Fibonacci.java to FibonacciLoop.java
debnath003 bba6ef8
Update FibonacciNumberGoldenRation.java
debnath003 ca78d1b
Update FibonacciNumberGoldenRationTest.java
debnath003 397f8c0
Update and rename FibonacciTest.java to FibonacciLoopTest.java
debnath003 d47ea12
Update FibonacciLoop.java
debnath003 21ebd33
Update FibonacciLoop.java
debnath003 f4f59d6
Update FibonacciNumberGoldenRation.java
debnath003 ff5df6a
docs: add missing dot
vil02 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
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,41 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import java.math.BigInteger; | ||
|
||
/** | ||
* This class provides methods for calculating Fibonacci numbers using BigInteger for large values of 'n'. | ||
*/ | ||
public final class Fibonacci { | ||
debnath003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private Fibonacci() { | ||
// Private constructor to prevent instantiation of this utility class. | ||
} | ||
|
||
/** | ||
* 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 calFibcompute(final int n) { | ||
debnath003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (n < 0) { | ||
throw new IllegalArgumentException("Input 'n' must be a non-negative integer."); | ||
debnath003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (n <= 1) { | ||
return BigInteger.valueOf(n); | ||
} | ||
|
||
BigInteger prev = BigInteger.ZERO; | ||
BigInteger current = BigInteger.ONE; | ||
|
||
for (int i = 2; i <= n; i++) { | ||
BigInteger next = prev.add(current); | ||
prev = current; | ||
current = next; | ||
} | ||
|
||
return current; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/thealgorithms/maths/FibonacciNumberGoldenRation.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,50 @@ | ||
package com.thealgorithms.maths; | ||
|
||
/** | ||
* This class provides methods for calculating Fibonacci numbers using Binet's formula. | ||
* Binet's formula is based on the golden ratio and allows computing Fibonacci numbers efficiently. | ||
* | ||
* @see <a href="https://en.wikipedia.org/wiki/Fibonacci_sequence#Binet's_formula">Binet's formula on Wikipedia</a> | ||
*/ | ||
public final class FibonacciNumberGoldenRation { | ||
private FibonacciNumberGoldenRation() { | ||
// Private constructor to prevent instantiation of this utility class. | ||
} | ||
|
||
/** | ||
* Compute the limit for 'n' that fits in a long data type. | ||
* Reducing the limit to 70 due to potential floating-point arithmetic errors | ||
* that may result in incorrect results for larger inputs. | ||
*/ | ||
public static final int MAX_ARG = 70; | ||
|
||
/** | ||
* Calculates the nth Fibonacci number using Binet's formula. | ||
* | ||
* @param n The index of the Fibonacci number to calculate. | ||
* @return The nth Fibonacci number as a long. | ||
* @throws IllegalArgumentException if the input 'n' is negative or exceeds the range of a long data type. | ||
*/ | ||
public static long nthFibonaccicompute(int n) { | ||
if (n < 0) { | ||
throw new IllegalArgumentException("Input 'n' must be a non-negative integer."); | ||
} | ||
|
||
if (n > MAX_ARG) { | ||
throw new IllegalArgumentException("Input 'n' is too large to fit into a long data type."); | ||
debnath003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (n <= 1) { | ||
return n; | ||
} | ||
|
||
// Calculate the nth Fibonacci number using the golden ratio formula | ||
final double sqrt5 = Math.sqrt(5); | ||
final double phi = (1 + sqrt5) / 2; | ||
final double psi = (1 - sqrt5) / 2; | ||
final double result = (Math.pow(phi, n) - Math.pow(psi, n)) / sqrt5; | ||
|
||
// Round to the nearest integer and return as a long | ||
return Math.round(result); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/com/thealgorithms/maths/FibonacciNumberGoldenRationTest.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,29 @@ | ||
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 FibonacciNumberGoldenRationTest { | ||
|
||
@Test | ||
public void returnsCorrectValues() { | ||
for (int n = 0; n <= FibonacciNumberGoldenRation.MAX_ARG; ++n) { | ||
final var actual = FibonacciNumberGoldenRation.nthFibonaccicompute(n); | ||
final var expected = Fibonacci.calFibcompute(n); | ||
assertEquals(expected, BigInteger.valueOf(actual)); | ||
} | ||
} | ||
|
||
@Test | ||
public void throwsIllegalArgumentExceptionForNegativeInput() { | ||
assertThrows(IllegalArgumentException.class, () -> { FibonacciNumberGoldenRation.nthFibonaccicompute(-1); }); | ||
} | ||
|
||
@Test | ||
public void throwsIllegalArgumentExceptionForLargeInput() { | ||
assertThrows(IllegalArgumentException.class, () -> { FibonacciNumberGoldenRation.nthFibonaccicompute(FibonacciNumberGoldenRation.MAX_ARG + 1); }); | ||
} | ||
} |
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; | ||
debnath003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.math.BigInteger; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class FibonacciTest { | ||
@Test | ||
public void checkValueAtZero() { | ||
assertEquals(BigInteger.ZERO, Fibonacci.calFibcompute(0)); | ||
} | ||
|
||
@Test | ||
public void checkValueAtOne() { | ||
assertEquals(BigInteger.ONE, Fibonacci.calFibcompute(1)); | ||
} | ||
|
||
@Test | ||
public void checkValueAtTwo() { | ||
assertEquals(BigInteger.ONE, Fibonacci.calFibcompute(2)); | ||
} | ||
|
||
@Test | ||
public void checkRecurrenceRelation() { | ||
for (int i = 0; i < 100; ++i) { | ||
assertEquals(Fibonacci.calFibcompute(i + 2), Fibonacci.calFibcompute(i + 1).add(Fibonacci.calFibcompute(i))); | ||
} | ||
} | ||
|
||
@Test | ||
public void checkNegativeInput() { | ||
assertThrows(IllegalArgumentException.class, () -> { Fibonacci.calFibcompute(-1); }); | ||
} | ||
} |
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.