-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d59c28a
commit cf10683
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters