refactor: Enhance docs, code, add tests in LucasSeries (#6749)

This commit is contained in:
Hardik Pawar
2025-10-15 14:37:49 +05:30
committed by GitHub
parent f460c601d3
commit c6497a23ec
2 changed files with 178 additions and 17 deletions

View File

@@ -1,38 +1,69 @@
package com.thealgorithms.maths;
/**
* https://en.wikipedia.org/wiki/Lucas_number
* Utility class for calculating Lucas numbers.
* The Lucas sequence is similar to the Fibonacci sequence but starts with 2 and
* 1.
* The sequence follows: L(n) = L(n-1) + L(n-2)
* Starting values: L(1) = 2, L(2) = 1
* Sequence: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, ...
*
* @see <a href="https://en.wikipedia.org/wiki/Lucas_number">Lucas Number</a>
* @author TheAlgorithms Contributors
*/
public final class LucasSeries {
private LucasSeries() {
}
/**
* Calculate nth number of Lucas Series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
* 123, ....) using recursion
* Calculate the nth Lucas number using recursion.
* Time Complexity: O(2^n) - exponential due to recursive calls
* Space Complexity: O(n) - recursion depth
*
* @param n nth
* @return nth number of Lucas Series
* @param n the position in the Lucas sequence (1-indexed, must be positive)
* @return the nth Lucas number
* @throws IllegalArgumentException if n is less than 1
*/
public static int lucasSeries(int n) {
return n == 1 ? 2 : n == 2 ? 1 : lucasSeries(n - 1) + lucasSeries(n - 2);
if (n < 1) {
throw new IllegalArgumentException("Input must be a positive integer. Provided: " + n);
}
if (n == 1) {
return 2;
}
if (n == 2) {
return 1;
}
return lucasSeries(n - 1) + lucasSeries(n - 2);
}
/**
* Calculate nth number of Lucas Series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
* 123, ....) using iteration
* Calculate the nth Lucas number using iteration.
* Time Complexity: O(n) - single loop through n iterations
* Space Complexity: O(1) - constant space usage
*
* @param n nth
* @return nth number of lucas series
* @param n the position in the Lucas sequence (1-indexed, must be positive)
* @return the nth Lucas number
* @throws IllegalArgumentException if n is less than 1
*/
public static int lucasSeriesIteration(int n) {
if (n < 1) {
throw new IllegalArgumentException("Input must be a positive integer. Provided: " + n);
}
if (n == 1) {
return 2;
}
if (n == 2) {
return 1;
}
int previous = 2;
int current = 1;
for (int i = 1; i < n; i++) {
for (int i = 2; i < n; i++) {
int next = previous + current;
previous = current;
current = next;
}
return previous;
return current;
}
}