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?
- 나열된 숫자들의 최소 공배수를 구하는 문제이다.
def gcd(a,b):
exp = 0;r=0
while ((a|b)&1 == 0):
exp += 1
a >>= 1
b >>= 1
while(a!=0 and b!=0):
while((a&1)==0):
a>>=1
while((b&1)==0):
b>>=1
if (a>b):
a = a-b
else:
b = b-a
if (a!=0):
r=a
else:
r=b
return r<<exp
def euler005(number=20):
num_list = [x for x in range(1,number+1)]
lcm = num_list[0]*num_list[1]//gcd(num_list[0], num_list[1])
for n in range(1, number):
lcm = lcm*num_list[n] // gcd(lcm, num_list[n])
print(lcm)