UNPKG

309 BPlain TextView Raw
1function sqrtPos(x: bigint): bigint {
2 var a = x, b = x+1n;
3 while(a<b) {
4 b = a;
5 a = (b + x/b)/2n;
6 }
7 return b;
8}
9
10/**
11 * Gives square root of value.
12 * @param x a bigint
13 */
14function sqrt(x: bigint): bigint {
15 if(x===0n) return 0n;
16 else return x>0n? sqrtPos(x) : null;
17}
18export default sqrt;