Greatest common divisor
Inputs
a and b are whole numbers from 1 to 500.
Your tasks
- Use a
whileloop that runs whilebis not 0. - Each round, replace
awithb, andbwith the remainder of the oldadivided by the oldb. - Complete
gcdso it returnsa. Return it, do not print it.
Example
With a = 48 and b = 18, gcd returns 6.
Rules
- Both inputs are at least 1.
Test data
TEST DATA 1: a is 48, b is 18
TEST DATA 2: a is 7, b is 13
Practice focus
While loops and early exits, for when you don't know how many rounds you need. Binary search, the Euclidean algorithm and graph search are all while loops with a clear stopping rule.