开发者

Can modulus/modulo work on big numbers in python?

开发者 https://www.devze.com 2023-02-20 00:00 出处:网络
Using mod on a big number, such as 600851475143, does not give a correct answer, anyone know why? print 600851475143 / 2.0

Using mod on a big number, such as 600851475143, does not give a correct answer, anyone know why?

print 600851475143 / 2.0
print 600851475143 % 2.0
print 4 / 2.0
print 4 % 2.0

600851475143 / 2.0 = 300425737572.0

600851475143 % 2.0 = 1.0

4 / 2.0 = 2.0

4 % 2.0 = 0.0

I was working on creating my own prime number function and it works fine for smaller开发者_如何学JAVA numbers, but modular seems to break when the numbers get bigger. I'm just messing around with python and the Euler challenge and have been banging my head against a wall for awhile now.

Thank you.


>>> print 600851475143 / 2
300425737571
>>> print 600851475143 % 2
1
>>> 4 / 2
2
>>> 4 % 2
0

Apart from that: your issue has nothing to do with big numbers - it's only about integer operations in Python.

0

精彩评论

暂无评论...
验证码 换一张
取 消