A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
|
|
@ -0,0 +1,9 @@
|
|||
>>> import fractions
|
||||
>>> def lcm(a,b): return abs(a * b) / fractions.gcd(a,b) if a and b else 0
|
||||
|
||||
>>> lcm(12, 18)
|
||||
36
|
||||
>>> lcm(-6, 14)
|
||||
42
|
||||
>>> assert lcm(0, 2) == lcm(2, 0) == 0
|
||||
>>>
|
||||
18
Task/Least-common-multiple/Python/least-common-multiple-2.py
Normal file
18
Task/Least-common-multiple/Python/least-common-multiple-2.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import operator
|
||||
from prime_decomposition import decompose
|
||||
|
||||
def lcm(a, b):
|
||||
if a and b:
|
||||
da = list(decompose(abs(a)))
|
||||
db = list(decompose(abs(b)))
|
||||
merge= da
|
||||
for d in da:
|
||||
if d in db: db.remove(d)
|
||||
merge += db
|
||||
return reduce(operator.mul, merge, 1)
|
||||
return 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
print( lcm(12, 18) ) # 36
|
||||
print( lcm(-6, 14) ) # 42
|
||||
assert lcm(0, 2) == lcm(2, 0) == 0
|
||||
19
Task/Least-common-multiple/Python/least-common-multiple-3.py
Normal file
19
Task/Least-common-multiple/Python/least-common-multiple-3.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
>>> def lcm(*values):
|
||||
values = set([abs(int(v)) for v in values])
|
||||
if values and 0 not in values:
|
||||
n = n0 = max(values)
|
||||
values.remove(n)
|
||||
while any( n % m for m in values ):
|
||||
n += n0
|
||||
return n
|
||||
return 0
|
||||
|
||||
>>> lcm(-6, 14)
|
||||
42
|
||||
>>> lcm(2, 0)
|
||||
0
|
||||
>>> lcm(12, 18)
|
||||
36
|
||||
>>> lcm(12, 18, 22)
|
||||
396
|
||||
>>>
|
||||
18
Task/Least-common-multiple/Python/least-common-multiple-4.py
Normal file
18
Task/Least-common-multiple/Python/least-common-multiple-4.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
>>> def lcm(p,q):
|
||||
p, q = abs(p), abs(q)
|
||||
m = p * q
|
||||
if not m: return 0
|
||||
while True:
|
||||
p %= q
|
||||
if not p: return m // q
|
||||
q %= p
|
||||
if not q: return m // p
|
||||
|
||||
|
||||
>>> lcm(-6, 14)
|
||||
42
|
||||
>>> lcm(12, 18)
|
||||
36
|
||||
>>> lcm(2, 0)
|
||||
0
|
||||
>>>
|
||||
Loading…
Add table
Add a link
Reference in a new issue