Skip to content

Commit 1420e2d

Browse files
* add LucasSeries
* add PerfectCube * PerfectSquare
1 parent 96345ad commit 1420e2d

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Maths/LucasSeries.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Maths;
2+
3+
/**
4+
* https://en.wikipedia.org/wiki/Lucas_number
5+
*/
6+
public class LucasSeries {
7+
public static void main(String[] args) {
8+
assert lucasSeries(1) == 2 && lucasSeriesIteration(1) == 2;
9+
assert lucasSeries(2) == 1 && lucasSeriesIteration(2) == 1;
10+
assert lucasSeries(3) == 3 && lucasSeriesIteration(3) == 3;
11+
assert lucasSeries(4) == 4 && lucasSeriesIteration(4) == 4;
12+
assert lucasSeries(5) == 7 && lucasSeriesIteration(5) == 7;
13+
assert lucasSeries(6) == 11 && lucasSeriesIteration(6) == 11;
14+
assert lucasSeries(11) == 123 && lucasSeriesIteration(11) == 123;
15+
16+
}
17+
18+
/**
19+
* Calculate nth number of lucas series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, ....) using recursion
20+
*
21+
* @param n nth
22+
* @return nth number of lucas series
23+
*/
24+
public static int lucasSeries(int n) {
25+
return n == 1 ? 2 : n == 2 ? 1 : lucasSeries(n - 1) + lucasSeries(n - 2);
26+
}
27+
28+
/**
29+
* Calculate nth number of lucas series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, ....) using iteration
30+
*
31+
* @param n nth
32+
* @return nth number of lucas series
33+
*/
34+
public static int lucasSeriesIteration(int n) {
35+
int previous = 2;
36+
int current = 1;
37+
for (int i = 1; i < n; i++) {
38+
int next = previous + current;
39+
previous = current;
40+
current = next;
41+
}
42+
return previous;
43+
}
44+
}

Maths/PerfectCube.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Maths;
2+
3+
/**
4+
* https://en.wikipedia.org/wiki/Cube_(algebra)
5+
*/
6+
public class PerfectCube {
7+
public static void main(String[] args) {
8+
assert !isPerfectCube(-1);
9+
assert isPerfectCube(0);
10+
assert isPerfectCube(1);
11+
assert !isPerfectCube(4);
12+
assert isPerfectCube(8);
13+
assert isPerfectCube(27);
14+
15+
}
16+
17+
/**
18+
* Check if a number is perfect cube or not
19+
*
20+
* @param number number to check
21+
* @return {@code true} if {@code number} is perfect cube, otherwise {@code false}
22+
*/
23+
public static boolean isPerfectCube(int number) {
24+
int a = (int) Math.pow(number, 1.0 / 3);
25+
return a * a * a == number;
26+
}
27+
}

Maths/PerfectSquare.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Maths;
2+
3+
/**
4+
* https://en.wikipedia.org/wiki/Perfect_square
5+
*/
6+
public class PerfectSquare {
7+
public static void main(String[] args) {
8+
assert !isPerfectSquare(-1);
9+
assert !isPerfectSquare(3);
10+
assert !isPerfectSquare(5);
11+
assert isPerfectSquare(9);
12+
assert isPerfectSquare(100);
13+
}
14+
15+
/**
16+
* Check if a number is perfect square number
17+
*
18+
* @param number the number to be checked
19+
* @return <tt>true</tt> if {@code number} is perfect square, otherwise <tt>false</tt>
20+
*/
21+
public static boolean isPerfectSquare(int number) {
22+
int sqrt = (int) Math.sqrt(number);
23+
return sqrt * sqrt == number;
24+
}
25+
}

0 commit comments

Comments
 (0)