0

通常,程序不会抛出小错误,但是当涉及到这些数字时,它会返回错误的除法结果

def leastCommonMultiple(n1, n2):
    a=n1
    b=n2
    while n2!=0:
        (n1, n2) = (n2, n1 % n2)
   print (n1) # greatest common divisior for given input is 5
   print(a*b) # for given numbers 231871064940156750
   return int((a*b)/n1) #wrong result    46374212988031352



numStr=input()
nums=numStr.split()
num1=int(nums[0])
num2=int(nums[1])
lcm=leastCommonMultiple(num1, num2)
print (lcm)


Input:
226553150 1023473145

Your output:
46374212988031352

Correct output:
46374212988031350

我写的解释:

leastCommonMultiple = (Num1*Num2)/greatestCommonDivisor

所以在while循环中我发现greatestCommonDivisor使用欧几里得方法

我用了这个公式(LCM = n1*n2/ GCD )

我希望我清楚地解释了这个问题。我能解决这个问题你能帮我吗?

4

1 回答 1

3

在 python 3 中使用 // 进行整数除法。我刚发现这个

于 2016-07-19T15:23:31.783 回答