Skip to content

Instantly share code, notes, and snippets.

@luke10x
Created October 17, 2014 18:31
Show Gist options
  • Save luke10x/527be70a0fdd5ac003c5 to your computer and use it in GitHub Desktop.
Save luke10x/527be70a0fdd5ac003c5 to your computer and use it in GitHub Desktop.
Python oneliners to find lowest common multiplier
#!/usr/bin/env python
print "="*80
#import sys
b = 24
a = 32
for i in range(1, min(a, b)):
if (a % i == 0 and b % i == 0):
gcd = i
#print "Found common divisor" + str(gcd)
lcm = a * b / gcd
print lcm
a = 32; b = 24; gcd = reduce((lambda x, y: y if a % y == 0 and b % y == 0 else x), range(1, min(a, b))); lcm = a * b / gcd ; print lcm
b = 24
a = 32
for i in range(min(a, b), 1, -1):
if (a % i == 0 and b % i == 0):
gcd = i
break
lcm = a * b / gcd
print lcm
a = 32; b = 24; l = lambda i: (lambda x: x if a % x == 0 and b % x == 0 else False)(next(i, 1)) or l(i); gcd = l(iter(range(min(a, b), 1, -1))); lcm = a * b / gcd; print lcm
import itertools; a = 32; b = 24; l = lambda i: (lambda x: x if a % x == 0 and b % x == 0 else False)(next(i, 1)) or l(i); gcd = l(itertools.count(start=min(a, b), step=-1)); lcm = a * b / gcd; print lcm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment