* add LucasSeries
* add PerfectCube * PerfectSquare
This commit is contained in:
parent
96345ad634
commit
1420e2d851
44
Maths/LucasSeries.java
Normal file
44
Maths/LucasSeries.java
Normal file
@ -0,0 +1,44 @@
|
||||
package Maths;
|
||||
|
||||
/**
|
||||
* https://en.wikipedia.org/wiki/Lucas_number
|
||||
*/
|
||||
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, 123, ....) using recursion
|
||||
*
|
||||
* @param n nth
|
||||
* @return nth number of lucas series
|
||||
*/
|
||||
public static int lucasSeries(int n) {
|
||||
return n == 1 ? 2 : n == 2 ? 1 : 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
|
||||
*
|
||||
* @param n nth
|
||||
* @return nth number of lucas series
|
||||
*/
|
||||
public static int lucasSeriesIteration(int n) {
|
||||
int previous = 2;
|
||||
int current = 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
int next = previous + current;
|
||||
previous = current;
|
||||
current = next;
|
||||
}
|
||||
return previous;
|
||||
}
|
||||
}
|
27
Maths/PerfectCube.java
Normal file
27
Maths/PerfectCube.java
Normal file
@ -0,0 +1,27 @@
|
||||
package Maths;
|
||||
|
||||
/**
|
||||
* https://en.wikipedia.org/wiki/Cube_(algebra)
|
||||
*/
|
||||
public class PerfectCube {
|
||||
public static void main(String[] args) {
|
||||
assert !isPerfectCube(-1);
|
||||
assert isPerfectCube(0);
|
||||
assert isPerfectCube(1);
|
||||
assert !isPerfectCube(4);
|
||||
assert isPerfectCube(8);
|
||||
assert isPerfectCube(27);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a number is perfect cube or not
|
||||
*
|
||||
* @param number number to check
|
||||
* @return {@code true} if {@code number} is perfect cube, otherwise {@code false}
|
||||
*/
|
||||
public static boolean isPerfectCube(int number) {
|
||||
int a = (int) Math.pow(number, 1.0 / 3);
|
||||
return a * a * a == number;
|
||||
}
|
||||
}
|
25
Maths/PerfectSquare.java
Normal file
25
Maths/PerfectSquare.java
Normal file
@ -0,0 +1,25 @@
|
||||
package Maths;
|
||||
|
||||
/**
|
||||
* https://en.wikipedia.org/wiki/Perfect_square
|
||||
*/
|
||||
public class PerfectSquare {
|
||||
public static void main(String[] args) {
|
||||
assert !isPerfectSquare(-1);
|
||||
assert !isPerfectSquare(3);
|
||||
assert !isPerfectSquare(5);
|
||||
assert isPerfectSquare(9);
|
||||
assert isPerfectSquare(100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a number is perfect square number
|
||||
*
|
||||
* @param number the number to be checked
|
||||
* @return <tt>true</tt> if {@code number} is perfect square, otherwise <tt>false</tt>
|
||||
*/
|
||||
public static boolean isPerfectSquare(int number) {
|
||||
int sqrt = (int) Math.sqrt(number);
|
||||
return sqrt * sqrt == number;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user