Created
September 17, 2015 09:00
-
-
Save checkaayush/8aaf9e2fe49beef6cd30 to your computer and use it in GitHub Desktop.
GCD of 'n' numbers using Python's built-in function 'reduce'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"GCD - Greatest Common Divisor" | |
def gcd(a, b): | |
'''Returns GCD of 2 numbers.''' | |
if not b: | |
return a | |
else: | |
return gcd(b, a % b) | |
def gcd_n(numbers): | |
'''Returns GCD of given n numbers. | |
Argument: numbers - List of given numbers | |
''' | |
return reduce(lambda x, y: gcd(x, y), numbers) | |
user_input = raw_input("Insert space separated numbers: ") | |
numbers = [int(n) for n in user_input.split()] | |
print "GCD of given numbers is %g" % (gcd_n(numbers)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment