Skip to content

Commit 5b536cc

Browse files
Merge branch 'master' into master
2 parents 03b19b7 + cb4760c commit 5b536cc

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Numerics;
3+
using Algorithms.Numeric;
4+
using NUnit.Framework;
5+
6+
namespace Algorithms.Tests.Numeric;
7+
8+
public static class FloorTests
9+
{
10+
[TestCase(0.0, 0)]
11+
[TestCase(1.1, 1)]
12+
[TestCase(1.9, 1)]
13+
[TestCase(1.0, 1)]
14+
[TestCase(-1.1, -2)]
15+
[TestCase(-1.9, -2)]
16+
[TestCase(-1.0, -1)]
17+
[TestCase(1000000000.1, 1000000000)]
18+
[TestCase(1, 1)]
19+
public static void GetsFloorVal<T>(T inputNum, T expected) where T : INumber<T>
20+
{
21+
// Act
22+
var result = Floor.FloorVal(inputNum);
23+
24+
// Assert
25+
Assert.That(result, Is.EqualTo(expected));
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Algorithms.Numeric;
2+
using NUnit.Framework;
3+
4+
namespace Algorithms.Tests.Numeric;
5+
6+
public static class PerfectCubeTests
7+
{
8+
[TestCase(-27, ExpectedResult = true)]
9+
[TestCase(27, ExpectedResult = true)]
10+
[TestCase(4, ExpectedResult = false)]
11+
[TestCase(64, ExpectedResult = true)]
12+
[TestCase(0, ExpectedResult = true)]
13+
[TestCase(1, ExpectedResult = true)]
14+
[TestCase(8, ExpectedResult = true)]
15+
[TestCase(9, ExpectedResult = false)]
16+
public static bool IsPerfectCube_ResultIsCorrect(int number)
17+
{
18+
// Act
19+
var result = PerfectCubeChecker.IsPerfectCube(number);
20+
21+
// Assert
22+
return result;
23+
}
24+
25+
[TestCase(-27, ExpectedResult = true)]
26+
[TestCase(27, ExpectedResult = true)]
27+
[TestCase(4, ExpectedResult = false)]
28+
[TestCase(64, ExpectedResult = true)]
29+
[TestCase(0, ExpectedResult = true)]
30+
[TestCase(1, ExpectedResult = true)]
31+
[TestCase(8, ExpectedResult = true)]
32+
[TestCase(9, ExpectedResult = false)]
33+
public static bool IsPerfectCubeBinarySearch_ResultIsCorrect(int number)
34+
{
35+
// Act
36+
var result = PerfectCubeChecker.IsPerfectCubeBinarySearch(number);
37+
38+
// Assert
39+
return result;
40+
}
41+
}

Algorithms/Numeric/Floor.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Numerics;
3+
4+
namespace Algorithms.Numeric;
5+
6+
/// <summary>
7+
/// Perform floor operation on a number.
8+
/// </summary>
9+
public static class Floor
10+
{
11+
/// <summary>
12+
/// Returns the largest integer less than or equal to the number.
13+
/// </summary>
14+
/// <typeparam name="T">Type of number.</typeparam>
15+
/// <param name="inputNum">Number to find the floor of.</param>
16+
/// <returns>Floor value of the number.</returns>
17+
public static T FloorVal<T>(T inputNum) where T : INumber<T>
18+
{
19+
T intPart = T.CreateChecked(Convert.ToInt32(inputNum));
20+
21+
return inputNum < intPart ? intPart - T.One : intPart;
22+
}
23+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric;
4+
5+
/// <summary>
6+
/// A perfect cube is an element of algebraic structure that is equal to the cube of another element.
7+
/// </summary>
8+
public static class PerfectCubeChecker
9+
{
10+
/// <summary>
11+
/// Checks if a number is a perfect cube or not.
12+
/// </summary>
13+
/// <param name="number">Number to check.</param>
14+
/// <returns>True if is a perfect cube; False otherwise.</returns>
15+
public static bool IsPerfectCube(int number)
16+
{
17+
if (number < 0)
18+
{
19+
number = -number;
20+
}
21+
22+
var cubeRoot = Math.Round(Math.Pow(number, 1.0 / 3.0));
23+
return Math.Abs(cubeRoot * cubeRoot * cubeRoot - number) < 1e-6;
24+
}
25+
26+
/// <summary>
27+
/// Checks if a number is a perfect cube or not using binary search.
28+
/// </summary>
29+
/// <param name="number">Number to check.</param>
30+
/// <returns>True if is a perfect cube; False otherwise.</returns>
31+
public static bool IsPerfectCubeBinarySearch(int number)
32+
{
33+
if (number < 0)
34+
{
35+
number = -number;
36+
}
37+
38+
int left = 0;
39+
int right = number;
40+
while (left <= right)
41+
{
42+
int mid = left + (right - left) / 2;
43+
int midCubed = mid * mid * mid;
44+
if (midCubed == number)
45+
{
46+
return true;
47+
}
48+
else if (midCubed < number)
49+
{
50+
left = mid + 1;
51+
}
52+
else
53+
{
54+
right = mid - 1;
55+
}
56+
}
57+
58+
return false;
59+
}
60+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ find more than one implementation for the same objective but using different alg
7575
* [Decomposition](./Algorithms/Numeric/Decomposition)
7676
* [LU Decomposition](./Algorithms/Numeric/Decomposition/LU.cs)
7777
* [Thin Singular Vector Decomposition](./Algorithms/Numeric/Decomposition/ThinSVD.cs)
78+
* [Floor](./Algorithms/Floor.cs)
7879
* [Greatest Common Divisor](./Algorithms/Numeric/GreatestCommonDivisor)
7980
* [Euclidean GCD](./Algorithms/Numeric/GreatestCommonDivisor/EuclideanGreatestCommonDivisorFinder.cs)
8081
* [Binary GCD](./Algorithms/Numeric/GreatestCommonDivisor/BinaryGreatestCommonDivisorFinder.cs)
@@ -89,6 +90,7 @@ find more than one implementation for the same objective but using different alg
8990
* [Keith Number Checker](./Algorithms/Numeric/KeithNumberChecker.cs)
9091
* [Pseudo-Inverse](./Algorithms/Numeric/Pseudoinverse/PseudoInverse.cs)
9192
* [Narcissistic Number Checker](./Algorithms/Numeric/NarcissisticNumberChecker.cs)
93+
* [Perfect Cube Checker](./Algorithms/Numeric/PerfectCubeChecker.cs)
9294
* [Perfect Number Checker](./Algorithms/Numeric/PerfectNumberChecker.cs)
9395
* [Perfect Square Checker](./Algorithms/Numeric/PerfectSquareChecker.cs)
9496
* [Euler Method](./Algorithms/Numeric/EulerMethod.cs)

0 commit comments

Comments
 (0)