| 15 | 15 | * @returns {bigint} |
| 16 | 16 | */ |
| 17 | 17 | export const square = (num) => { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 18 | // Edge Case: Input is not a number |
| 19 | if (typeof num !== 'number' || isNaN(num)) { |
| 20 | throw new Error('square must be between 1 and 64'); |
| 21 | } |
| 22 | |
| 23 | // Edge Case: Input is not an integer |
| 24 | if (!Number.isInteger(num)) { |
| 25 | throw new Error('square must be between 1 and 64'); |
| 26 | } |
| 27 | |
| 28 | // Edge Case: Square number less than 1 |
| 29 | if (num < 1) { |
| 30 | throw new Error('square must be between 1 and 64'); |
| 31 | } |
| 32 | |
| 33 | // Edge Case: Square number greater than 64 |
| 34 | if (num > 64) { |
| 35 | throw new Error('square must be between 1 and 64'); |
| 36 | } |
| 37 | |
| 38 | // Calculate grains on the square (2^(n-1)) |
| 39 | return 1n << BigInt(num - 1); |
| 19 | 40 | }; |
| 20 | 41 | |
| 21 | 42 | /** |
| 22 | 43 | * @returns {bigint} |
| 23 | 44 | */ |
| 24 | 45 | export const total = () => { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 46 | // The total number of grains is 2^64 - 1 |
| 47 | // This is a mathematical sum of 1 + 2 + 4 + ... + 2^63 |
| 48 | return (1n << 64n) - 1n; |
| 26 | 49 | }; |
| 50 | |
| 51 | // Handled Edge Cases: square number less than 1, square number greater than 64, non-integer square numbers, handling very large numbers that exceed standard integer limits |