Python/maths/find_lcm.py

35 lines
692 B
Python
Raw Normal View History

"""Find Least Common Multiple."""
# https://en.wikipedia.org/wiki/Least_common_multiple
def find_lcm(num_1, num_2):
"""Find the least common multiple of two numbers.
>>> find_lcm(5,2)
10
>>> find_lcm(12,76)
228
"""
2019-10-05 05:14:13 +00:00
if num_1 >= num_2:
max_num = num_1
else:
2019-10-05 05:14:13 +00:00
max_num = num_2
lcm = max_num
while True:
2019-10-05 05:14:13 +00:00
if (lcm % num_1 == 0) and (lcm % num_2 == 0):
break
lcm += max_num
return lcm
def main():
"""Use test numbers to run the find_lcm algorithm."""
2019-10-05 05:14:13 +00:00
num_1 = int(input().strip())
num_2 = int(input().strip())
print(find_lcm(num_1, num_2))
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
main()