Intermediate · Functions · 109 of 264
Greatest common divisor
Write gcd(a, b) with Euclid's method: while b is not 0, replace (a, b) with (b, a % b); then a is the answer. Do not use math.gcd.
Input: two positive whole numbers, one per line. Output: their greatest common divisor.
Examples
Input
12 18
Output
6
Input
17 5
Output
1
Input
100 75
Output
25
One more test checks other inputs when you press Check.
Stuck? The idea behind functions
A function, made with def, gives a name to a piece of code, so it can be used again: its parameters are the values it is given, return hands an answer back.
A function that returns lets the caller decide what to do with the answer: print it, compare it, use it in a sum.
def is_even(n):
return n % 2 == 0
print(is_even(4))
print(is_even(7))Watch for: print inside a function is not return: the caller gets None. And the code after return in the same block never runs.