using System;
using System.Numerics;
public class Program
{
public static BigInteger Sqrt(BigInteger bi) {
if (bi == 0) return 0;
int length = Convert.ToInt32(Math.Ceiling(BigInteger.Log(bi, 10)));
BigInteger root = BigInteger.One << (length / 2);
while (!isSqrt(bi, root)) {
root += bi / root;
root /= 2;
}
return root;
}
private static Boolean isSqrt(BigInteger bi, BigInteger root) {
BigInteger lowerBound = root * root;
BigInteger upperBound = (root + 1) * (root + 1);
return bi >= lowerBound && bi < upperBound;
}
public static void Main()
{
BigInteger bigInteger = BigInteger.Parse("58820136703657669922151936");
try {
Console.WriteLine("Sqrt: " + Sqrt(bigInteger));
}
catch (ArithmeticException e) {
Console.WriteLine(e.Message);
}
}
}
// 7669428707775.9364176426041243971
/*
run:
Sqrt: 7669428707775
*/