tobe

Finished
1111//
1212
1313/**
14 * @param {number} num
15 * @returns {bigint}
14 * Calculate the number of grains on a specific square
15 * @param {number} num - The square number (1-64)
16 * @returns {bigint} - The number of grains on that square
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 used to handle large numbers
31 return 1n << BigInt(num - 1);
1932};
2033
2134/**
22 * @returns {bigint}
35 * Calculate the total number of grains on the chessboard
36 * @returns {bigint} - The total number of grains
2337 */
2438export const total = () => {
25 throw new Error('Remove this line and implement the function');
39 // The sum of a geometric series with first term a=1 and common ratio r=2
40 // for n=64 terms is (2^64) - 1
41 // Using bit shifting to calculate 2^64 and then subtracting 1
42 return (1n << 64n) - 1n;
2643};
44
45// Handled Edge Cases: Square number less than 1, square number greater than 64
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.