DEV Community

Discussion on: Project Euler #5 - Finding the Smallest Multiple

Collapse
 
aspittel profile image
Ali Spittel

Here's mine!

def greatest_common_denominator(a, b):
    while b:      
        a, b = b, a % b
    return a


def least_common_multiple(a, b):
    return (a * b) / greatest_common_denominator(a, b)


def least_common_multiple_range(li):
    if len(li) == 2:
        return least_common_multiple(li[0], li[1])
    else:
        check = li.pop()
        return least_common_multiple(check, least_common_multiple_range(li))


print least_common_multiple_range(range(1, 21))
Collapse
 
maxart2501 profile image
Massimo Artizzu

The most significant part here is that you can compute the least common multiple by computing the greatest common denominator and using it to divide the product of the two numbers.

I was about to propose a similar solution (in JavaScript) but yours is sufficient 👍
(Yes, the language itself isn't important for me. Just the mathematical challenge.)

Collapse
 
1258632 profile image
by

Please do propose your version in javascript. Certainly it will not be excessive

Collapse
 
tweedoriginal profile image
TWEEDOriginal

Can you please explain your code