RosettaCodeData/Task/Isqrt-integer-square-root-of-X/Mathematica/isqrt-integer-square-root-of-x.math
2023-07-01 13:44:08 -04:00

20 lines
300 B
Text

ClearAll[ISqrt]
ISqrt[x_Integer?NonNegative] := Module[{q = 1, z, r, t},
While[q <= x,
q *= 4
];
z = x;
r = 0;
While[q > 1,
q = Quotient[q, 4];
t = z - r - q;
r /= 2;
If[t >= 0,
z = t;
r += q
];
];
r
]
ISqrt /@ Range[65]
Column[ISqrt /@ (7^Range[1, 73])]