Post Reply 
Find LCM of 2 numbers without using GCD
07-30-2018, 06:13 PM
Post: #7
RE: Find LCM of 2 numbers without using GCD
(07-30-2018 04:41 PM)Thomas Klemm Wrote:  But then why not using Jean-Marc Baillard's solution: GCD?

Exactly ! Why brute force LCM ?

For lcm(54321, 12345), a simple gcd derived lcm is 2600X faster !
(Speed relative to the no modulus brute forced lcm, post #3)

Code:
def gcd(a, b):
    while b:
        a = a % b
        if not a: return b
        b = b % a
    return a

def lcm(a,b):
    g = gcd(a, b)
    return g and a // g * b
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Find LCM of 2 numbers without using GCD - Albert Chan - 07-30-2018 06:13 PM



User(s) browsing this thread: 1 Guest(s)