Last active
March 7, 2020 10:49
-
-
Save woodRock/674d5d6b8842d2a979112cac3fbe313d to your computer and use it in GitHub Desktop.
This problem was asked by Amazon. Given n numbers, find the greatest common denominator between them. For example, given the numbers [42, 56, 14], return 14.
This file contains 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
def gcd a, b | |
return a if b == 0 | |
return gcd(b, a % b) | |
end | |
def gcd_in_list list | |
list = list.sort | |
grt = 0 | |
list.each do |i| | |
grt = gcd(i, grt) | |
end | |
puts grt | |
end | |
list = [ 42, 56, 14] | |
puts gcd_in_list list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment