Q) 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?
Solution: Python: Quite Fast
==========================
num = 20
def GCD1(num1,num2):
if num1>num2:
num1,num2 = num2,num1
x = [m for m in range(2,num1+1) if not (num1%m)]
y = []
for n in range(num1,1,-1):
if not(num2%n)and (n in x):
y.append(n)
return y[0]
def GCD(num1,num2):
if num1>num2:
return GCD1(num1-num2,num2)
elif num2>num1:
return GCD1(num1, num2-num1)
else:
return num1
def LCM(num1,num2):
x = abs(num1*num2)
y = GCD(num1,num2)
if y == None:
ans = x
else:
ans = x/y
return ans
def SmallMul():
k = ans = num
for i in range(k,k/2,-1):
ans = LCM(ans,i)
return ans
print SmallMul()
No comments:
Post a Comment