From 1420e2d851c71a3ee2d25f8fac10fd90042ecbef Mon Sep 17 00:00:00 2001 From: shellhub Date: Sun, 16 Aug 2020 21:16:08 +0800 Subject: [PATCH] * add LucasSeries * add PerfectCube * PerfectSquare --- Maths/LucasSeries.java | 44 ++++++++++++++++++++++++++++++++++++++++ Maths/PerfectCube.java | 27 ++++++++++++++++++++++++ Maths/PerfectSquare.java | 25 +++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 Maths/LucasSeries.java create mode 100644 Maths/PerfectCube.java create mode 100644 Maths/PerfectSquare.java diff --git a/Maths/LucasSeries.java b/Maths/LucasSeries.java new file mode 100644 index 00000000..8fa7097b --- /dev/null +++ b/Maths/LucasSeries.java @@ -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; + } +} diff --git a/Maths/PerfectCube.java b/Maths/PerfectCube.java new file mode 100644 index 00000000..1db0dc3e --- /dev/null +++ b/Maths/PerfectCube.java @@ -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; + } +} diff --git a/Maths/PerfectSquare.java b/Maths/PerfectSquare.java new file mode 100644 index 00000000..7484b2fd --- /dev/null +++ b/Maths/PerfectSquare.java @@ -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 true if {@code number} is perfect square, otherwise false + */ + public static boolean isPerfectSquare(int number) { + int sqrt = (int) Math.sqrt(number); + return sqrt * sqrt == number; + } +} \ No newline at end of file