mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +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) + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,22 +5,12 @@ package com.thealgorithms.maths;
|
||||
*/
|
||||
public class LucasSeries {
|
||||
|
||||
public static void main(String[] args) {
|
||||
assert lucasSeries(1) == 2 && lucasSeriesIteration(1) == 2;
|
||||
assert lucasSeries(2) == 1 && lucasSeriesIteration(2) == 1;
|
||||
assert lucasSeries(3) == 3 && lucasSeriesIteration(3) == 3;
|
||||
assert lucasSeries(4) == 4 && lucasSeriesIteration(4) == 4;
|
||||
assert lucasSeries(5) == 7 && lucasSeriesIteration(5) == 7;
|
||||
assert lucasSeries(6) == 11 && lucasSeriesIteration(6) == 11;
|
||||
assert lucasSeries(11) == 123 && lucasSeriesIteration(11) == 123;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate nth number of lucas series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
|
||||
* Calculate nth number of Lucas Series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
|
||||
* 123, ....) using recursion
|
||||
*
|
||||
* @param n nth
|
||||
* @return nth number of lucas series
|
||||
* @return nth number of Lucas Series
|
||||
*/
|
||||
public static int lucasSeries(int n) {
|
||||
return n == 1
|
||||
@ -29,7 +19,7 @@ public class LucasSeries {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate nth number of lucas series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
|
||||
* Calculate nth number of Lucas Series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
|
||||
* 123, ....) using iteration
|
||||
*
|
||||
* @param n nth
|
||||
|
@ -7,18 +7,9 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class Median {
|
||||
|
||||
public static void main(String[] args) {
|
||||
assert median(new int[] { 0 }) == 0;
|
||||
assert median(new int[] { 1, 2 }) == 1.5;
|
||||
assert median(new int[] { 4, 1, 3, 2 }) == 2.5;
|
||||
assert median(new int[] { 1, 3, 3, 6, 7, 8, 9 }) == 6;
|
||||
assert median(new int[] { 1, 2, 3, 4, 5, 6, 8, 9 }) == 4.5;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate average median
|
||||
*
|
||||
* @param values number series
|
||||
* @param values sorted numbers to find median of
|
||||
* @return median of given {@code values}
|
||||
*/
|
||||
public static double median(int[] values) {
|
||||
|
Reference in New Issue
Block a user