Skip to content

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 142 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
990737f
Create FibonacciNumber.java
debnath003 Oct 1, 2023
986de53
Update FibonacciNumber.java
debnath003 Oct 1, 2023
f0cb961
Update FibonacciNumber.java
debnath003 Oct 1, 2023
f10ba98
Merge branch 'master' into master
debnath003 Oct 1, 2023
458a67a
Merge branch 'master' into master
debnath003 Oct 1, 2023
af5ead3
Merge branch 'master' into master
debnath003 Oct 3, 2023
85aea40
Merge branch 'master' into master
debnath003 Oct 4, 2023
10b4020
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 7, 2023
9b591dc
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 7, 2023
83b6fd0
Update FibonacciNumber.java
debnath003 Oct 7, 2023
5fd14ae
Merge branch 'master' into master
debnath003 Oct 7, 2023
52d5d48
Update FibonacciNumber.java
debnath003 Oct 7, 2023
d923df5
Update FibonacciNumber.java
debnath003 Oct 7, 2023
d95ff92
Update FibonacciNumber.java
debnath003 Oct 8, 2023
d79c527
Create FibonacciNumberTest.java
debnath003 Oct 8, 2023
e03c75e
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
838cd9e
Merge branch 'master' into master
debnath003 Oct 8, 2023
0c12169
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
b8fb3e3
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
c8fe427
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
61c8ae8
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
3597f5e
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
9dc18ba
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
cc58ea7
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
03e1c1b
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
673bb54
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
11acae5
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
01834f9
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
494a5de
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
ba3a9b1
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
01a33ff
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
4d6df56
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
837f306
Update FibonacciNumber.java
debnath003 Oct 8, 2023
25e22bf
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
565a687
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
aaceb6f
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 8, 2023
253cda0
Update FibonacciNumber.java
debnath003 Oct 8, 2023
aa0265e
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
37cf8ee
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
eb52e31
Update FibonacciNumber.java
debnath003 Oct 8, 2023
21684e6
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
e9a5780
Merge branch 'master' into master
debnath003 Oct 8, 2023
75e31ed
Update FibonacciNumberTest.java
debnath003 Oct 8, 2023
2bff558
Delete src/main/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 Oct 8, 2023
dd6078c
Create FibonacciNumberTest.java
debnath003 Oct 8, 2023
9bb2b54
Update FibonacciNumber.java
debnath003 Oct 9, 2023
ff21124
Update FibonacciNumberTest.java
debnath003 Oct 9, 2023
86f83ab
Update FibonacciNumber.java
debnath003 Oct 9, 2023
7c85b73
Update FibonacciNumber.java
debnath003 Oct 9, 2023
f17dc7b
Merge branch 'master' into master
debnath003 Oct 9, 2023
e580f0e
Update FibonacciNumber.java
debnath003 Oct 9, 2023
151651f
Update FibonacciNumber.java
debnath003 Oct 9, 2023
bf53e85
Update FibonacciNumberTest.java
debnath003 Oct 9, 2023
45a64db
Merge branch 'master' into master
debnath003 Oct 10, 2023
529d9c5
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 Oct 10, 2023
ab9942e
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 10, 2023
e8000d6
Create FibonacciCalculator.java
debnath003 Oct 10, 2023
556aa8f
Update FibonacciNumberTest.java
debnath003 Oct 10, 2023
e34a19b
Update and rename FibonacciCalculator.java to FibCalc.java
debnath003 Oct 10, 2023
1a8fef2
Update FibonacciNumberTest.java
debnath003 Oct 10, 2023
56465dc
Update FibCalc.java
debnath003 Oct 10, 2023
361fdc9
Update FibonacciNumber.java
debnath003 Oct 10, 2023
2547ada
Merge branch 'master' into master
debnath003 Oct 11, 2023
3cf874b
Delete src/test/java/com/thealgorithms/maths/FibCalc.java
debnath003 Oct 11, 2023
bcae36f
Create FibCalc.java
debnath003 Oct 11, 2023
bbb3a6a
Update FibonacciNumberTest.java
debnath003 Oct 11, 2023
5fb25f4
Update FibCalc.java
debnath003 Oct 11, 2023
6fd2d76
Update FibonacciNumberTest.java
debnath003 Oct 11, 2023
2805d43
Update FibonacciNumber.java
debnath003 Oct 11, 2023
7fe6a1e
Update FibonacciNumberTest.java
debnath003 Oct 11, 2023
b666be8
Update FibonacciNumber.java
debnath003 Oct 11, 2023
be514bf
Update FibonacciNumber.java
debnath003 Oct 11, 2023
d85c849
Update FibonacciNumber.java
debnath003 Oct 12, 2023
960f0c0
Update FibonacciNumber.java
debnath003 Oct 12, 2023
6600421
Update FibonacciNumberTest.java
debnath003 Oct 12, 2023
c005dda
Update FibonacciNumber.java
debnath003 Oct 12, 2023
a77bdb3
Merge branch 'master' into master
debnath003 Oct 12, 2023
dbd1438
fix: use proper name
vil02 Oct 12, 2023
35e7f93
fix: use proper class name
vil02 Oct 12, 2023
34602ed
tests: add `returnsCorrectValues`
vil02 Oct 12, 2023
339c9a9
Update and rename FibCalc.java to Fibonacci.java
debnath003 Oct 12, 2023
66607ff
Update Fibonacci.java
debnath003 Oct 12, 2023
8969263
Update FibonacciNumber.java
debnath003 Oct 12, 2023
3bc67d5
Update FibonacciNumberTest.java
debnath003 Oct 12, 2023
049a818
Update FibonacciNumberTest.java
debnath003 Oct 12, 2023
ab6b1c3
Update Fibonacci.java
debnath003 Oct 12, 2023
859e0bd
Update FibonacciNumber.java
debnath003 Oct 12, 2023
5460411
Update and rename FibCalcTest.java to FibonacciTest.java
debnath003 Oct 12, 2023
b430ff4
Update FibonacciNumber.java
debnath003 Oct 12, 2023
89642b1
Update Fibonacci.java
debnath003 Oct 12, 2023
4f0b0f4
Update Fibonacci.java
debnath003 Oct 12, 2023
d2c738f
Update Fibonacci.java
debnath003 Oct 12, 2023
8a9b65c
Update FibonacciTest.java
debnath003 Oct 12, 2023
53545a6
Update Fibonacci.java
debnath003 Oct 12, 2023
62cd87c
Update src/main/java/com/thealgorithms/maths/Fibonacci.java
debnath003 Oct 13, 2023
227e08c
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 13, 2023
7fc9e74
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 Oct 13, 2023
e629bb3
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 Oct 13, 2023
ed272c4
Merge branch 'master' into master
debnath003 Oct 13, 2023
766b40c
Update FibonacciTest.java
debnath003 Oct 13, 2023
a328240
Update FibonacciNumberTest.java
debnath003 Oct 13, 2023
e7367b5
Update FibonacciNumberTest.java
debnath003 Oct 13, 2023
106e5b4
Update FibonacciTest.java
debnath003 Oct 13, 2023
8789440
Update src/main/java/com/thealgorithms/maths/Fibonacci.java
debnath003 Oct 13, 2023
ae09b5f
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 13, 2023
bc0112b
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 Oct 13, 2023
567606a
Update src/test/java/com/thealgorithms/maths/FibonacciTest.java
debnath003 Oct 13, 2023
191970b
Update src/main/java/com/thealgorithms/maths/Fibonacci.java
debnath003 Oct 13, 2023
7bc0b10
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 13, 2023
8bc8465
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 13, 2023
5581ea9
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 13, 2023
b223196
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 13, 2023
b9ac542
Update src/main/java/com/thealgorithms/maths/FibonacciNumber.java
debnath003 Oct 13, 2023
ce069f2
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 Oct 13, 2023
a943471
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 Oct 13, 2023
fd03500
Update src/test/java/com/thealgorithms/maths/FibonacciNumberTest.java
debnath003 Oct 13, 2023
a09b2c3
Update FibonacciNumber.java
debnath003 Oct 13, 2023
324086a
Update FibonacciNumber.java
debnath003 Oct 13, 2023
77f4635
Update Fibonacci.java
debnath003 Oct 13, 2023
044a9c4
Update FibonacciNumber.java
debnath003 Oct 13, 2023
04539a0
Update and rename FibonacciNumber.java to FibonacciNumberGoldenRation…
debnath003 Oct 13, 2023
f054ad6
Update and rename FibonacciNumberTest.java to FibonacciNumberGoldenRa…
debnath003 Oct 13, 2023
f348278
Update Fibonacci.java
debnath003 Oct 13, 2023
466c4d5
Update FibonacciNumberGoldenRation.java
debnath003 Oct 13, 2023
440da96
Update FibonacciNumberGoldenRationTest.java
debnath003 Oct 13, 2023
f722307
Update FibonacciTest.java
debnath003 Oct 13, 2023
6f43ecb
Update Fibonacci.java
debnath003 Oct 13, 2023
7985170
Update FibonacciNumberGoldenRationTest.java
debnath003 Oct 13, 2023
902ba1a
Update FibonacciNumberGoldenRationTest.java
debnath003 Oct 13, 2023
c356a1d
Update FibonacciNumberGoldenRation.java
debnath003 Oct 13, 2023
a0af070
Update FibonacciNumberGoldenRation.java
debnath003 Oct 13, 2023
e6d1bd9
Update FibonacciNumberGoldenRationTest.java
debnath003 Oct 13, 2023
e6da345
Update FibonacciNumberGoldenRationTest.java
debnath003 Oct 13, 2023
faca3e1
Update src/main/java/com/thealgorithms/maths/FibonacciNumberGoldenRat…
debnath003 Oct 13, 2023
6d4f92b
Update and rename Fibonacci.java to FibonacciLoop.java
debnath003 Oct 13, 2023
bba6ef8
Update FibonacciNumberGoldenRation.java
debnath003 Oct 13, 2023
ca78d1b
Update FibonacciNumberGoldenRationTest.java
debnath003 Oct 13, 2023
397f8c0
Update and rename FibonacciTest.java to FibonacciLoopTest.java
debnath003 Oct 13, 2023
d47ea12
Update FibonacciLoop.java
debnath003 Oct 13, 2023
21ebd33
Update FibonacciLoop.java
debnath003 Oct 13, 2023
f4f59d6
Update FibonacciNumberGoldenRation.java
debnath003 Oct 13, 2023
ff5df6a
docs: add missing dot
vil02 Oct 13, 2023
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
41 changes: 41 additions & 0 deletions src/main/java/com/thealgorithms/maths/FibonacciLoop.java
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 FibonacciLoop {

private FibonacciLoop() {
// 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 compute(final int n) {
if (n < 0) {
throw new IllegalArgumentException("Input 'n' must be a non-negative integer.");
}

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;
}
}
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 compute(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 big to give accurate result.");
}

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);
}
}
36 changes: 36 additions & 0 deletions src/test/java/com/thealgorithms/maths/FibonacciLoopTest.java
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 FibonacciLoopTest {
@Test
public void checkValueAtZero() {
assertEquals(BigInteger.ZERO, FibonacciLoop.compute(0));
}

@Test
public void checkValueAtOne() {
assertEquals(BigInteger.ONE, FibonacciLoop.compute(1));
}

@Test
public void checkValueAtTwo() {
assertEquals(BigInteger.ONE, FibonacciLoop.compute(2));
}

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

@Test
public void checkNegativeInput() {
assertThrows(IllegalArgumentException.class, () -> { FibonacciLoop.compute(-1); });
}
}
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.compute(n);
final var expected = FibonacciLoop.compute(n);
assertEquals(expected, BigInteger.valueOf(actual));
}
}

@Test
public void throwsIllegalArgumentExceptionForNegativeInput() {
assertThrows(IllegalArgumentException.class, () -> { FibonacciNumberGoldenRation.compute(-1); });
}

@Test
public void throwsIllegalArgumentExceptionForLargeInput() {
assertThrows(IllegalArgumentException.class, () -> { FibonacciNumberGoldenRation.compute(FibonacciNumberGoldenRation.MAX_ARG + 1); });
}
}