Last active
December 19, 2015 08:49
-
-
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!
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
| #!/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