Skip to content

Commit

Permalink
Add perfect cube numeric algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulofalcao2002 committed Oct 30, 2024
1 parent d59c28a commit cf10683
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Algorithms.Tests/Numeric/PerfectCubeTests.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
60 changes: 60 additions & 0 deletions Algorithms/Numeric/PerfectCubeChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;

namespace Algorithms.Numeric;

/// <summary>
/// A perfect cube is an element of algebraic structure that is equal to the cube of another element.
/// </summary>
public static class PerfectCubeChecker
{
/// <summary>
/// Checks if a number is a perfect cube or not.
/// </summary>
/// <param name="number">Number to check.</param>
/// <returns>True if is a perfect cube; False otherwise.</returns>
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;
}

/// <summary>
/// Checks if a number is a perfect cube or not using binary search.
/// </summary>
/// <param name="number">Number to check.</param>
/// <returns>True if is a perfect cube; False otherwise.</returns>
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;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cf10683

Please sign in to comment.