GCD Function
Python
Easy
4 views
Problem Description
Read two integers a and b. Write function gcd(a,b) using Euclid and output it (always non-negative).
Input Format
One line: a b.
Output Format
One integer gcd.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<2: sys.exit(0)
a=int(p[0]); b=int(p[1])
def gcd(x,y):
x=abs(x); y=abs(y)
while y:
x,y=y,x%y
return x
sys.stdout.write(str(gcd(a,b)))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!