forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLeonardoNumber.java
More file actions
79 lines (73 loc) · 2.49 KB
/
LeonardoNumber.java
File metadata and controls
79 lines (73 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.thealgorithms.maths;
/**
* Utility class for calculating Leonardo Numbers.
* <p>
* Leonardo numbers are a sequence of numbers defined by the recurrence:
* L(n) = L(n-1) + L(n-2) + 1, with L(0) = 1 and L(1) = 1
* <p>
* The sequence begins: 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, ...
* <p>
* This class provides both a recursive implementation and an optimized
* iterative
* implementation for calculating Leonardo numbers.
*
* @see <a href="https://en.wikipedia.org/wiki/Leonardo_number">Leonardo Number
* - Wikipedia</a>
* @see <a href="https://oeis.org/A001595">OEIS A001595</a>
*/
public final class LeonardoNumber {
private LeonardoNumber() {
}
/**
* Calculates the nth Leonardo Number using recursion.
* <p>
* Time Complexity: O(2^n) - exponential due to repeated calculations
* Space Complexity: O(n) - due to recursion stack
* <p>
* Note: This method is not recommended for large values of n due to exponential
* time complexity.
* Consider using {@link #leonardoNumberIterative(int)} for better performance.
*
* @param n the index of the Leonardo Number to calculate (must be non-negative)
* @return the nth Leonardo Number
* @throws IllegalArgumentException if n is negative
*/
public static int leonardoNumber(int n) {
if (n < 0) {
throw new IllegalArgumentException("Input must be non-negative. Received: " + n);
}
if (n == 0 || n == 1) {
return 1;
}
return leonardoNumber(n - 1) + leonardoNumber(n - 2) + 1;
}
/**
* Calculates the nth Leonardo Number using an iterative approach.
* <p>
* This method provides better performance than the recursive version for large
* values of n.
* <p>
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* @param n the index of the Leonardo Number to calculate (must be non-negative)
* @return the nth Leonardo Number
* @throws IllegalArgumentException if n is negative
*/
public static int leonardoNumberIterative(int n) {
if (n < 0) {
throw new IllegalArgumentException("Input must be non-negative. Received: " + n);
}
if (n == 0 || n == 1) {
return 1;
}
int previous = 1;
int current = 1;
for (int i = 2; i <= n; i++) {
int next = current + previous + 1;
previous = current;
current = next;
}
return current;
}
}