Return to Snippet

Revision: 71064
at December 26, 2016 05:41 by amirteymuri


Initial Code
def lcm(*args):
    """Calculates lcm of args"""
    #find the largest of numbers:
    biggest = max(args)
    #the list of the numbers without the largest:
    rest = [n for n in args if n != biggest]
    #the factor is to multiply with the biggest as long as the result of them is not divisble by all of the numbers in the rest:
    factor = 1 
    while True:
        #check if biggest is divisble by all in the rest:
        ans = False in [(biggest * factor) % n == 0 for n in rest]
        #if so the clm is found: break the loop and return it, otherwise increment the factor by 1 and try again:
        if not ans:
            break
        factor += 1
    biggest *= factor
    return "lcm of {0} is {1}".format(args, biggest)

Initial URL

                                

Initial Description
Calculates the lcm of 2 or more numbers (based on a self-made algorithm)

Initial Title
least common multiple

Initial Tags
python

Initial Language
Python