I'm trying to perform the following calculation:

$$ a\pmod m $$

where both $a$ and $m$ are numbers larger than $32$ bits. However, I'm only able to perform calculations on $32$ bit numbers.

So I was wondering if there is a way to compute arbitrary precision modulo.

  • 1
    $\begingroup$ Of course there is a way... But you'll have to use more than 32 bits if the input (and in particularly $m$) is larger than 32 bits. $\endgroup$ – barak manos Dec 2 '16 at 17:20
  • 1
    $\begingroup$ Just how is this related to math? As long as math is concerned, there is absolutely nothing special about 32 or any other particular number of bits. Essentially, this is a programming question. I vaguely remember hearing that there is a special SE site dedicated to that area. Long story short, you have to define your own type and redefine the arithmetic operations for it. $\endgroup$ – Ivan Neretin Dec 2 '16 at 17:37
  • $\begingroup$ Java and .NET framework (C# and others) provide BigInteger library. In C/C++ LibGMP is best for multi-precision arithmetic and number theory. Btw I think this question is out of the scope of this site and should be asked in SO or SciComp. $\endgroup$ – kub0x Dec 2 '16 at 18:54

The most simple way is using Python 2.5+ or Python 3.0+.

Just perform standard math operations and any number which exceeds the boundaries of 32-bit math will be automatically and transparently converted to a bignum.

Moreover, Python is an interpreted language so you may use it as a calculator - just write an expression an press Enter to see the result.

Modulo in Python is represented by the operator %.


>>> 999999999999999999999999999999 % 333333333333333333333

gives the result


Not the answer you're looking for? Browse other questions tagged or ask your own question.