joseph

Finished
1111//
1212
1313/**
14 * @param {number} num
14 * Calculate the number of grains on a specific square
15 * @param {number} num - The square number (1-64)
1516 * @returns {bigint}
1617 */
1718export const square = (num) => {
18 throw new Error('Remove this line and implement the function');
19 // Edge Case: Square number is less than 1
20 if (num < 1) {
21 throw new Error('square must be between 1 and 64');
22 }
23
24 // Edge Case: Square number is greater than 64
25 if (num > 64) {
26 throw new Error('square must be between 1 and 64');
27 }
28
29 // Using bit shifting to calculate 2^(n-1)
30 // BigInt is needed because the numbers get very large
31 return 1n << BigInt(num - 1);
1932};
2033
2134/**
35 * Calculate the total number of grains on the chessboard
2236 * @returns {bigint}
2337 */
2438export const total = () => {
25 throw new Error('Remove this line and implement the function');
39 // The sum of 2^0 + 2^1 + 2^2 + ... + 2^63 is 2^64 - 1
40 // This is a mathematical formula for the sum of a geometric series
41 return (1n << 64n) - 1n;
2642};
43
44// Handled Edge Cases: Square number less than 1, square number greater than 64
45// Handled Edge Cases: None (function always returns the same value)
Test NameStatus
grains on square 1
Pass
grains on square 2
Pass
grains on square 3
Pass
grains on square 4
Pass
grains on square 16
Pass
grains on square 32
Pass
grains on square 64
Pass
square 0 raises an exception
Pass
negative square raises an exception
Pass
square greater than 64 raises an exception
Pass
returns the total number of grains on the board
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.