diff --git a/Algorithms.Tests/Numeric/PerfectCubeTests.cs b/Algorithms.Tests/Numeric/PerfectCubeTests.cs new file mode 100644 index 00000000..9bf03aea --- /dev/null +++ b/Algorithms.Tests/Numeric/PerfectCubeTests.cs @@ -0,0 +1,41 @@ +using Algorithms.Numeric; +using NUnit.Framework; + +namespace Algorithms.Tests.Numeric; + +public static class PerfectCubeTests +{ + [TestCase(-27, ExpectedResult = true)] + [TestCase(27, ExpectedResult = true)] + [TestCase(4, ExpectedResult = false)] + [TestCase(64, ExpectedResult = true)] + [TestCase(0, ExpectedResult = true)] + [TestCase(1, ExpectedResult = true)] + [TestCase(8, ExpectedResult = true)] + [TestCase(9, ExpectedResult = false)] + public static bool IsPerfectCube_ResultIsCorrect(int number) + { + // Act + var result = PerfectCubeChecker.IsPerfectCube(number); + + // Assert + return result; + } + + [TestCase(-27, ExpectedResult = true)] + [TestCase(27, ExpectedResult = true)] + [TestCase(4, ExpectedResult = false)] + [TestCase(64, ExpectedResult = true)] + [TestCase(0, ExpectedResult = true)] + [TestCase(1, ExpectedResult = true)] + [TestCase(8, ExpectedResult = true)] + [TestCase(9, ExpectedResult = false)] + public static bool IsPerfectCubeBinarySearch_ResultIsCorrect(int number) + { + // Act + var result = PerfectCubeChecker.IsPerfectCubeBinarySearch(number); + + // Assert + return result; + } +} diff --git a/Algorithms/Numeric/PerfectCubeChecker.cs b/Algorithms/Numeric/PerfectCubeChecker.cs new file mode 100644 index 00000000..cf3eebd6 --- /dev/null +++ b/Algorithms/Numeric/PerfectCubeChecker.cs @@ -0,0 +1,60 @@ +using System; + +namespace Algorithms.Numeric; + +/// +/// A perfect cube is an element of algebraic structure that is equal to the cube of another element. +/// +public static class PerfectCubeChecker +{ + /// + /// Checks if a number is a perfect cube or not. + /// + /// Number to check. + /// True if is a perfect cube; False otherwise. + public static bool IsPerfectCube(int number) + { + if (number < 0) + { + number = -number; + } + + var cubeRoot = Math.Round(Math.Pow(number, 1.0 / 3.0)); + return Math.Abs(cubeRoot * cubeRoot * cubeRoot - number) < 1e-6; + } + + /// + /// Checks if a number is a perfect cube or not using binary search. + /// + /// Number to check. + /// True if is a perfect cube; False otherwise. + public static bool IsPerfectCubeBinarySearch(int number) + { + if (number < 0) + { + number = -number; + } + + int left = 0; + int right = number; + while (left <= right) + { + int mid = left + (right - left) / 2; + int midCubed = mid * mid * mid; + if (midCubed == number) + { + return true; + } + else if (midCubed < number) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + } + + return false; + } +} diff --git a/README.md b/README.md index 8cce7949..bfd6df40 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ find more than one implementation for the same objective but using different alg * [Keith Number Checker](./Algorithms/Numeric/KeithNumberChecker.cs) * [Pseudo-Inverse](./Algorithms/Numeric/Pseudoinverse/PseudoInverse.cs) * [Narcissistic Number Checker](./Algorithms/Numeric/NarcissisticNumberChecker.cs) + * [Perfect Cube Checker](./Algorithms/Numeric/PerfectCubeChecker.cs) * [Perfect Number Checker](./Algorithms/Numeric/PerfectNumberChecker.cs) * [Perfect Square Checker](./Algorithms/Numeric/PerfectSquareChecker.cs) * [Euler Method](./Algorithms/Numeric/EulerMethod.cs)