Created
June 12, 2018 22:26
-
-
Save mjs3339/94992ffbc75e96a4bfbdb5f82c9c947e to your computer and use it in GitHub Desktop.
C# BigInteger Square Root Approximation
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
public BigInteger Sqrt(BigInteger n) | |
{ | |
var q = BigInteger.One << ((int)BigInteger.Log(n, 2) >> 1); | |
var m = BigInteger.Zero; | |
while (BigInteger.Abs(q - m) >= 1) | |
{ | |
m = q; | |
q = (m + n / m) >> 1; | |
} | |
return q; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment