The Russian Peasant

MediumAcc. 89.2%
+30 XP 12

Peasant Multiplication

This ancient method multiplies two numbers without using a multiplication table.

  1. Doubling column: Keep doubling the first number.
  2. Halving column: Keep halving the second number (round down) until it reaches 1.
  3. 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.

  1. Initialize result to 0.
  2. Use a while loop that runs as long as b is greater than 0.
  3. In each step:
    • If b is odd, add a to result.
    • Double a (a * 2).
    • Halve b (Math.floor(b / 2)).
  4. After the loop, print the final result.

01EXAMPLE 1

Inputa=5, b=7
Output35

Explanation: 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
JavaScript
Loading...
3 Hidden

Input Arguments

a5
b7

Expected Output

35

Click RUN to test your solution