The GCD Navigator

MediumAcc. 90.3%
+25 XP 10

Shared Factors

The Greatest Common Divisor (GCD) is the largest positive integer that divides each of two or more integers without a remainder.

The Assignment

Your function receives parameters a and b.

  1. Use a while loop that continues as long as b is not 0.
  2. In each iteration:
    • Keep a temporary copy of b.
    • Update b to be the remainder of a divided by b (a % b).
    • Update a to be the temporary copy of b.
  3. After b becomes 0, print the value of a.

01EXAMPLE 1

Inputa=48, b=18
Output6

Explanation: Divisors of 48: 1, 2, 3, 4, 6, 8, 12, 16, 24, 48. Divisors of 18: 1, 2, 3, 6, 9, 18. Greatest shared is 6.

Constraints

  • Use the iterative Euclidean algorithm.
  • Do not use utility libraries.
MathLoops
JavaScript
Loading...
3 Hidden

Input Arguments

a48
b18

Expected Output

6

Click RUN to test your solution