mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
Add More Tests (#4148)
This commit is contained in:

committed by
GitHub

parent
f35e9a7d81
commit
7779c18ef6
@ -1,20 +1,23 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
/**
|
||||
* https://en.wikipedia.org/wiki/Leonardo_number
|
||||
*/
|
||||
public class LeonardoNumber {
|
||||
|
||||
/**
|
||||
* Calculate nth Leonardo Number (1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, ...)
|
||||
*
|
||||
* @param n the index of Leonardo Number to calculate
|
||||
* @return nth number of Leonardo sequences
|
||||
*/
|
||||
public static int leonardoNumber(int n) {
|
||||
if (n < 0) {
|
||||
return 0;
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
if (n == 0 || n == 1) {
|
||||
return 1;
|
||||
}
|
||||
return (leonardoNumber(n - 1) + leonardoNumber(n - 2) + 1);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
for (int i = 0; i < 20; i++) {
|
||||
System.out.print(leonardoNumber(i) + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user