48 lines
987 B
JavaScript
48 lines
987 B
JavaScript
module.exports = {
|
|
min(...nums) {
|
|
if (nums.length === 0) {
|
|
throw new Error('invalid arguments');
|
|
}
|
|
return nums.reduce((a, b) => (a < b ? a : b));
|
|
},
|
|
max(...nums) {
|
|
if (nums.length === 0) {
|
|
throw new Error('invalid arguments');
|
|
}
|
|
return nums.reduce((a, b) => (a > b ? a : b));
|
|
},
|
|
abs(num) {
|
|
if (num < 0n) {
|
|
return num * -1n;
|
|
}
|
|
return num;
|
|
},
|
|
sqrt(num) {
|
|
if (num < 0n) {
|
|
throw new Error('no sqrt of negative');
|
|
}
|
|
if (num <= 1n) {
|
|
return num;
|
|
}
|
|
|
|
let x0 = num >> 1n;
|
|
let x1 = (x0 + num / x0) >> 1n;
|
|
|
|
while (x1 < x0) {
|
|
x0 = x1;
|
|
x1 = (x0 + num / x0) >> 1n;
|
|
}
|
|
|
|
return x0;
|
|
},
|
|
// todo: implement longer string parsing, should be simple-ish to do with
|
|
// some looping and multiplication
|
|
parseInt(string, radix) {
|
|
if (string.length > 10) {
|
|
console.log(string);
|
|
throw new Error('todo');
|
|
}
|
|
return BigInt(parseInt(string, radix));
|
|
},
|
|
};
|