본문 바로가기

파이썬

[Project Euler]5. Smallest multiple

[문제]
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 positive number that is evenly divisible by all of the numbers from 1 to 20?

 

[풀이]
total = 1
for i in range(1,21):
    result = []
    for j in range(2,21):
        if total %j==0 and i%j==0:
            while total%j==0 and i%j==0:
                i=int(i/j)
                total=int(total/j)
                result.append(j)
    for a in result:
        total = total * a
    total = total * i

print(total)

 

'''
갑자기 머리가 번뜩여서 두 문제나 풀었다
이 문제는 굉장히 어렵게 풀었는데

맨 처음엔 각 수의 약수를 모두 구해서 중복되는 약수는 지우고... 하지만 중복되지만 필요한(2^3같은..) 약수는 남기고..
이런 식으로 풀이를 하다가 막혔다.

여러 풀이를 생각하다가 앞에서부터 차근차근 최소공약수를 구하고
그 최소공약수의 최소공약수.. 이런 식으로 해결해보기로 했다.

이렇게 앞에서부터 차근차근 하기로 하면 은근히 쉽게 풀 수 있었다.
'''