python中查找最不常见的multi时出现长整数除法错误

2024-05-23 14:13:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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 )

我希望我把问题解释清楚。我能解决这个问题吗?你能帮我吗?你知道吗


Tags: forinput错误givenintprintwhilen2