Skip to content

Instantly share code, notes, and snippets.

@mmcclimon
Last active December 19, 2015 08:49
Show Gist options
  • Save mmcclimon/5929034 to your computer and use it in GitHub Desktop.
Save mmcclimon/5929034 to your computer and use it in GitHub Desktop.
This file runs in both Ruby and Python...I didn't realize they were quite so similar!
#!/usr/bin/env python
#!/usr/bin/env ruby
# take your pick!
def gcd(a, b):
# greatest common divisor...Euclidian algorithm
while b != 0:
tmp = b
b = a % tmp
a = tmp
return a
def lcm(a, b):
# least common multiple (via GCD)
return (a * b) / gcd(a, b)
def lcm_arr(args):
# least common multiple from a list
if len(args) == 1:
return args[0]
else:
return lcm(args[0], lcm_arr(args[1:]))
print lcm_arr(range(1,21))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment