Elaborating on the comments. In Python 3, true division, /
, converts its arguments to floats. In your example, the true answer of lcm(226553150, 1023473145)
is 46374212988031350
. By looking at bin(46374212988031350)
you can verify that this is a 56 bit number. When you compute 226553150*1023473145/5
(5 is the gcd) you get 4.637421298803135e+16
. Documentation suggests that such floats only have 53 bits of precision. Since 53 < 56, you have lost information. Using //
avoids this. Somewhat counterintuitively, in cases like this it is "true" division which is actually false.
By the way, a useful module when dealing with exact calculations involving large integers is fractions (*):
from fractions import gcd
def lcm(a,b):
return a*b // gcd(a,b)
>>> lcm(226553150,1023473145)
46374212988031350
(*) I just noticed that the documentation on fractions
says this about its gcd
: "Deprecated since version 3.5: Use math.gcd() instead", but I decided to keep the reference to fractions
since it is still good to know about it and you might be using a version prior to 3.5.