Ceasar's Mind

Follow me: @Ceasar_Bautista

Relationship between Least Common Multiple and Greatest Common Factor

with 2 comments

Never realized this before. Kind of cool.

def gcd(a, b):
    pfa = prime_factors(a)
    pfb = prime_factors(b)
    common = set(pfa) & set(pfb)
    return reduce(operator.mul, [i ** min(pfa.count(i), pfb.count(i)) for i in common])

def lcm(a, b):
    '''Get the least common multiple of two numbers.'''
    pfa = prime_factors(a)
    pfb = prime_factors(b)
    common = set(pfa) & set(pfb)
    return reduce(operator.mul, [i ** max(pfa.count(i), pfb.count(i)) for i in common])

Written by Ceasar Bautista

2011/07/01 at 17:30

Posted in Uncategorized

Tagged with ,

2 Responses

Subscribe to comments with RSS.

  1. This is why all CIS students should take number theory.

    Mish

    2011/07/01 at 22:32

  2. also this shows why lcm(a,b) = ab/gcd(a,b)

    Chao Xu

    2011/10/23 at 06:32


Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 69 other followers