LearnFrenzy
Sign in Take a free test

Recursion · Predict the Output · Question 9

Euclid's GCD recursively Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

What is the output of the following Python program?

main.py
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)

print(gcd(48, 18), gcd(18, 48), gcd(7, 13), gcd(0, 9))
  • A 6 18 1 0
  • B 6 6 1 0
  • C 18 6 1 9
  • D 6 6 1 9 Correct
Correct Answer: Option D

gcd(18, 48) first becomes gcd(48, 18) because 18 % 48 is 18. gcd(0, 9) → gcd(9, 0) → 9.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links