From 957f633c931009e88b31bfd39b2aad3bc9e93758 Mon Sep 17 00:00:00 2001 From: Taranjeet Singh Kalsi Date: Sat, 29 Oct 2022 11:57:17 +0530 Subject: [PATCH] Added function and fixed bug in PerfectCube.java (#3655) * Added another function to PerfectCube.java Added another function to PerfectCube.java and fixed a testing mistake in line number 9 * Created PerfectCubeTest.java Created PerfectCubeTest.java * fixed PerfectCubeTest.java * Fixed bug in PerfectCube.java Fixed bug in PerfectCube.java in isPerfectCube() function for negative numbers. Now It gives the correct output for perfect negative numbers. * removed main() in PerfectCube.java --- .../com/thealgorithms/maths/PerfectCube.java | 22 ++++++++----- .../thealgorithms/maths/PerfectCubeTest.java | 33 +++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/PerfectCubeTest.java diff --git a/src/main/java/com/thealgorithms/maths/PerfectCube.java b/src/main/java/com/thealgorithms/maths/PerfectCube.java index 820b1720..0fae7889 100644 --- a/src/main/java/com/thealgorithms/maths/PerfectCube.java +++ b/src/main/java/com/thealgorithms/maths/PerfectCube.java @@ -5,15 +5,6 @@ package com.thealgorithms.maths; */ 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 * @@ -22,7 +13,20 @@ public class PerfectCube { * {@code false} */ public static boolean isPerfectCube(int number) { + number = Math.abs(number); // converting negative number to positive number int a = (int) Math.pow(number, 1.0 / 3); return a * a * a == number; } + + /** + * Check if a number is perfect cube or not by using Math.cbrt function + * + * @param number number to check + * @return {@code true} if {@code number} is perfect cube, otherwise + * {@code false} + */ + public static boolean isPerfectCubeMathCbrt(int number) { + double cubeRoot = Math.cbrt(number); + return cubeRoot == (int) cubeRoot; + } } diff --git a/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java b/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java new file mode 100644 index 00000000..806f5493 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java @@ -0,0 +1,33 @@ +package com.thealgorithms.maths; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class PerfectCubeTest { + + @Test + public void perfectCube() { + + Assertions.assertTrue(PerfectCube.isPerfectCube(-27)); + Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(-27)); + Assertions.assertTrue(PerfectCube.isPerfectCube(-1)); + Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(-1)); + Assertions.assertTrue(PerfectCube.isPerfectCube(0)); + Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(0)); + Assertions.assertTrue(PerfectCube.isPerfectCube(1)); + Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(1)); + Assertions.assertTrue(PerfectCube.isPerfectCube(8)); + Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(8)); + Assertions.assertTrue(PerfectCube.isPerfectCube(27)); + Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(27)); + + Assertions.assertFalse(PerfectCube.isPerfectCube(-9)); + Assertions.assertFalse(PerfectCube.isPerfectCubeMathCbrt(-9)); + Assertions.assertFalse(PerfectCube.isPerfectCube(2)); + Assertions.assertFalse(PerfectCube.isPerfectCubeMathCbrt(2)); + Assertions.assertFalse(PerfectCube.isPerfectCube(4)); + Assertions.assertFalse(PerfectCube.isPerfectCubeMathCbrt(4)); + Assertions.assertFalse(PerfectCube.isPerfectCube(30)); + Assertions.assertFalse(PerfectCube.isPerfectCubeMathCbrt(30)); + } +}