-1

I'm doing mod for big number in python 2.7.3 and python is giving me wrong answer as shown below. If number is small, then it gives correct answer.

Can anybody explain why this happens and how I can fix in python?? below simple script showing small number mod shows correct answer but big number mod gives wrong answer.

root# cat mod.py 

bigNum=46623871433466988486147975697094761425185742805822717249007
smallNum=717249007

print "bigNum % 3: " + str(bigNum % 3)

print "smallNum % 3: " + str(smallNum % 3)

root# python mod.py

bigNum % 3: 0  
smallNum % 3: 1

Appreciate for help in advance.

kmas
  • 6,401
  • 13
  • 40
  • 62
user3022431
  • 63
  • 1
  • 1
  • 5
  • 3
    Where is the wrong answer? See http://stackoverflow.com/questions/2664301/how-does-modulus-divison-work. – sPaz Nov 22 '13 at 16:28
  • I'm getting the correct answer 0 too... what's the expected result and why? :) – Joachim Isaksson Nov 22 '13 at 16:32
  • 2
    guys, shame on me. this is first time posting question and I was too quick to think python is giving me wrong answer. Indeed python is giving me correct answer. Sorry guys and thanks for correcting me quickly. I'll be more prudent next time. – user3022431 Nov 22 '13 at 16:50

1 Answers1

7

What do you expect it to return? It looks right to me:

>>> bigNum=46623871433466988486147975697094761425185742805822717249007
>>> bigNum % 3
0L
>>> divmod(bigNum, 3)
(15541290477822329495382658565698253808395247601940905749669L, 0L)
>>> _[0] * 3 == bigNum
True

Note that the sum of digits is divisible by 3, so bigNum is also:

>>> sum(map(int, str(bigNum)))
288
>>> 288 % 3
0
Tim Peters
  • 67,464
  • 13
  • 126
  • 132