Finding the LCM of a range of numbers

前端 未结 14 883
时光说笑
时光说笑 2020-12-08 05:03

I read an interesting DailyWTF post today, \"Out of All The Possible Answers...\" and it interested me enough to dig up the original forum post where it was submitted. This

14条回答
  •  借酒劲吻你
    2020-12-08 06:00

    This is probably the cleanest, shortest answer (both in terms of lines of code) that I've seen so far.

    def gcd(a,b): return b and gcd(b, a % b) or a
    def lcm(a,b): return a * b / gcd(a,b)
    
    n = 1
    for i in xrange(1, 21):
        n = lcm(n, i)
    

    source : http://www.s-anand.net/euler.html

提交回复
热议问题