寻找最小公倍数时 python 中的长整数除法错误
long integers division error in python while finding least common multiple
通常情况下,程序不会为小数抛出错误,但是当涉及到这些数字时,它 returns 错误的除法结果
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 )
希望我把问题解释清楚了。请问你能帮我解决这个问题吗?
在 python 3 中使用 // 进行整数除法。我刚发现这个
通常情况下,程序不会为小数抛出错误,但是当涉及到这些数字时,它 returns 错误的除法结果
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 )
希望我把问题解释清楚了。请问你能帮我解决这个问题吗?
在 python 3 中使用 // 进行整数除法。我刚发现这个