Skip to content

Instantly share code, notes, and snippets.

@woodRock
Last active March 7, 2020 10:49
Show Gist options
  • Save woodRock/674d5d6b8842d2a979112cac3fbe313d to your computer and use it in GitHub Desktop.
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.
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