Skip to content

Commit 247c984

Browse files
committed
posting lowest common multiple
1 parent 41ec1e4 commit 247c984

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lcm.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# computes Lowest Common Multiple LCM / Least Common Denominator LCD
2+
# useful for adding and subtracting fractions
3+
4+
# 2 numbers
5+
def lcm(num1, num2):
6+
if num1 > num2:
7+
num1, num2 = num2, num1
8+
for x in range (num2, num1 * num2 + 1, num2):
9+
if x % num1 == 0:
10+
return x
11+
12+
# list of 3 numbers
13+
def lcm3(nums):
14+
nums.sort()
15+
worst = nums[0]*nums[1]*nums[2]
16+
17+
for x in range(nums[2], worst + 1, nums[2]):
18+
if x % nums[0] == 0 and x % nums[1] == 0:
19+
return x
20+
21+
print(str(lcm(7, 12)))
22+
23+
nums = [3, 2, 16]
24+
print(str(lcm3(nums)))

0 commit comments

Comments
 (0)