Skip to content

Commit d381c4e

Browse files
authored
Add Newton's Square Root Calculation Method (TheAlgorithms#357)
1 parent 35a304b commit d381c4e

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Numerics;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Numeric;
6+
7+
public class NewtonSquareRootTests
8+
{
9+
private static readonly object[] CalculateSquareRootInput =
10+
{
11+
new object[] {BigInteger.One, BigInteger.One},
12+
new object[] {new BigInteger(221295376), new BigInteger(14876)},
13+
new object[] {new BigInteger(2530995481), new BigInteger(50309)},
14+
new object[] {new BigInteger(3144293476), new BigInteger(56074)},
15+
new object[] {new BigInteger(3844992064), new BigInteger(62008)},
16+
new object[] {new BigInteger(5301150481), new BigInteger(72809)},
17+
new object[] {new BigInteger(5551442064), new BigInteger(74508)},
18+
new object[] {new BigInteger(6980435401), new BigInteger(83549)},
19+
new object[] {new BigInteger(8036226025), new BigInteger(89645)},
20+
};
21+
22+
[TestCaseSource(nameof(CalculateSquareRootInput))]
23+
public void CalculateSquareRootTest(BigInteger number, BigInteger result)
24+
{
25+
Assert.That(NewtonSquareRoot.Calculate(number), Is.EqualTo(result));
26+
}
27+
28+
[Test]
29+
public void CalculateSquareRootOfZero()
30+
{
31+
Assert.That(NewtonSquareRoot.Calculate(0), Is.EqualTo(BigInteger.Zero));
32+
}
33+
34+
[Test]
35+
public void CalculateSquareRootNegativeNumber()
36+
{
37+
Assert.Throws(Is.TypeOf<ArgumentException>()
38+
.And.Message.EqualTo("Cannot calculate the square root of a negative number."),
39+
delegate
40+
{
41+
NewtonSquareRoot.Calculate(BigInteger.MinusOne);
42+
});
43+
}
44+
}

Algorithms/NewtonSquareRoot.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Numerics;
3+
4+
namespace Algorithms;
5+
6+
public static class NewtonSquareRoot
7+
{
8+
public static BigInteger Calculate(BigInteger number)
9+
{
10+
if (number < 0)
11+
{
12+
throw new ArgumentException("Cannot calculate the square root of a negative number.");
13+
}
14+
15+
if (number == 0)
16+
{
17+
return BigInteger.Zero;
18+
}
19+
20+
var bitLength = Convert.ToInt32(Math.Ceiling(BigInteger.Log(number, 2)));
21+
BigInteger root = BigInteger.One << (bitLength / 2);
22+
23+
while (!IsSquareRoot(number, root))
24+
{
25+
root += number / root;
26+
root /= 2;
27+
}
28+
29+
return root;
30+
}
31+
32+
private static bool IsSquareRoot(BigInteger number, BigInteger root)
33+
{
34+
var lowerBound = root * root;
35+
return number >= lowerBound && number <= lowerBound + root + root;
36+
}
37+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ find more than one implementation for the same objective but using different alg
8282
* [Miller-Rabin primality check](./Algorithms/Numeric/MillerRabinPrimalityChecker.cs)
8383
* [KrishnamurthyNumberChecker](./Algorithms/Numeric/KrishnamurthyNumberChecker.cs)
8484
* [Automorphic Number](./Algorithms/Numeric/AutomorphicNumber.cs)
85+
* [Newton's Square Root Calculation](./Algorithms/NewtonSquareRoot.cs)
8586
* [Searches](./Algorithms/Search)
8687
* [A-Star](./Algorithms/Search/AStar/)
8788
* [Binary Search](./Algorithms/Search/BinarySearcher.cs)

0 commit comments

Comments
 (0)