-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Square root calculations sometimes rounding up instead of rounding down due to the high numbers of decimals.
this can be fixed by this;
sqrtBigInt: {
name: "Square Root",
example: "85027756688728992039343472749973395489496426478679146914594092850470138746682004844846731574411929455787504015372183412744082517126948688460316362207223994823057005387889005747347064036786977021248868",
solver: (data) => {
const n = BigInt(data);
if (n < 0n) {
return 'ERROR';
}
if (n < 2n) {
return n;
}
// Newton's method for integer square root
let x = n;
let y = (x + 1n) / 2n;
while (y < x) {
x = y;
y = (n / x + x) / 2n;
}
return x;
}
},
Metadata
Metadata
Assignees
Labels
No labels