| 11 | 11 | // |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | | * @param {number} num |
| 15 | | * @returns {bigint} |
| 14 | * Calculate the number of grains on a specific square of a chessboard. |
| 15 | * Each square has twice as many grains as the previous square. |
| 16 | * |
| 17 | * @param {number} num - The square number (1-64) |
| 18 | * @returns {bigint} The number of grains on that square |
| 19 | * @throws {Error} If the square number is not between 1 and 64 |
| 16 | 20 | */ |
| 17 | 21 | export const square = (num) => { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 22 | // Edge Case: Square number is less than 1 |
| 23 | if (num < 1) { |
| 24 | throw new Error('square must be between 1 and 64'); |
| 25 | } |
| 26 | |
| 27 | // Edge Case: Square number is greater than 64 |
| 28 | if (num > 64) { |
| 29 | throw new Error('square must be between 1 and 64'); |
| 30 | } |
| 31 | |
| 32 | // Calculate grains using the formula: 2^(n-1) |
| 33 | // We subtract 1 from num because square 1 has 2^0 = 1 grain |
| 34 | return 1n << BigInt(num - 1); |
| 35 | |
| 36 | // Handled Edge Cases: Square number less than 1, square number greater than 64 |
| 19 | 37 | }; |
| 20 | 38 | |
| 21 | 39 | /** |
| 22 | | * @returns {bigint} |
| 40 | * Calculate the total number of grains on the entire chessboard. |
| 41 | * Uses the geometric series sum formula: 2^64 - 1 |
| 42 | * |
| 43 | * @returns {bigint} The total number of grains on the chessboard |
| 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 calculated as (2^64) - 1 which equals 18446744073709551615 |
| 48 | return (1n << 64n) - 1n; |
| 49 | |
| 50 | // Handled Edge Cases: None (this function always returns the same constant value) |
| 26 | 51 | }; |