본문 바로가기

프로그래밍/알고리즘

[EP 005] least common multiple for a list of numbers

반응형
####################################################
# http://projecteuler.net
#
# Problem 5
#
# 2520 is the smallest number that can be divided 
# by each of the numbers from 1 to 10 without any 
# remainder.
#
# What is the smallest number that is evenly
# divisible by all of the numbers from 1 to 20?
####################################################
# Author    : DwYoon
# Date      : 2007 04 12

N = 20

lst = [ i for i in range(1,N+1) ]
for i, n in enumerate(lst):
    for j in range(i+1, len(lst)):
        if lst[j]%n == 0:
            lst[j] = lst[j]//n

#print lst
product = 1
for n in lst:
    product = product*n
print( product )
728x90