The Russian Peasant
MediumAcc. 89.2%
+30 XP 12
Peasant Multiplication
This ancient method multiplies two numbers without using a multiplication table.
- Doubling column: Keep doubling the first number.
- Halving column: Keep halving the second number (round down) until it reaches 1.
- Sum the numbers in the "doubling" column that sit across from odd numbers in the "halving" column.
The Assignment
Your function receives parameters a and b.
- Initialize
resultto 0. - Use a
whileloop that runs as long asbis greater than 0. - In each step:
- If
bis odd, addatoresult. - Double
a(a * 2). - Halve
b(Math.floor(b / 2)).
- If
- After the loop, print the final
result.
01EXAMPLE 1
Input
a=5, b=7Output
35Explanation: 7(odd) -> add 5. 3(odd) -> add 10. 1(odd) -> add 20. Total = 35.
Constraints
- Use the while loop doubling/halving logic.
- Do not use * for the final result.
MathLoops
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
a5
b7
Expected Output
35
Click RUN to test your solution