Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #LCM.py by Chris Clarke
- #Python3
- #14.05.2018
- """
- Asked on Yahoo! Answers:
- How can i find lcm of n numbers in python without using any builtin
- function and also not using recursion function?
- """
- """greatest common divisor"""
- def gcd (a, b):
- while b != 0:
- t = b
- b = a % b
- a = t
- return a
- """least common multiple"""
- def lcm (a, b):
- r = gcd (a, b)
- return a // r * b
- def main():
- print ("Program to find the least common multiple of N numbers.")
- n = []
- while True:
- a = int (input ("Enter a number: (-1 to quit) : "))
- if a >= 0:
- n.append (a)
- else:
- break
- result = n[0]
- for x in range (1, len (n)):
- result = lcm (result, n[x])
- print ("The LCM of {0} is {1}" .format (n, result))
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement