Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Created June 12, 2018 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs3339/94992ffbc75e96a4bfbdb5f82c9c947e to your computer and use it in GitHub Desktop.
Save mjs3339/94992ffbc75e96a4bfbdb5f82c9c947e to your computer and use it in GitHub Desktop.
C# BigInteger Square Root Approximation
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