This site is supported by donations to The OEIS Foundation.

Modular arithmetic

From OeisWiki
Jump to: navigation, search

Modular arithmetic operates on the remainders of numbers divided by a given modulus rather than on the numbers themselves. For example, rather than computing 2134 + 87659 = 89793, we might compute 4 + 9 = 3 by a modulus of 10. Thus modular arithmetic reduces the unmanageably infinite to the manageably finite. Addition, subtraction, multiplication and exponentiation can all readily be done in modular arithmetic.

With being an integer, being a modulus and being the remainder of (the number such that , here we are unconcerned with the value of the integer ), the notation is either or In our earlier example, . (This also demonstrates that if the modulo is the base of place-value numeration, then taking the remainder is the same as fetching the least significant digit of the integer part). The infinity of has been reduced to . Obviously when .

The simplest modular arithmetic uses a modulus of 2; this is the arithmetic of odd and even numbers. Thus, without examining each possible pair of an odd number and an even number (which is of course impossible) we know that an odd number plus an even number is odd but an odd number times an even number is even, because but .

Perhaps the moduli of most practical use are 12 and 24 (and thus modular arithmetic is sometimes called "clock arithmetic"). Suppose there is a task which takes a machine precisely 98 hours to complete, and we start the machine on this task at 7:00 AM on Monday. We could compute , then and then to figure out that, uninterrupted, the machine will finish at 9:00 AM on Friday. Or we could just observe that since , the finishing time will be just two hours after the starting time (but on a different day, of course).

Number theory is generally concerned with prime numbers as moduli. Consider for example Fermat's little theorem. Usually remainders are expressed as positive numbers, but mathematicians often use when (or as a shortcut for when considering arbitrary moduli).

Most calculators don't do modular arithmetic. Instead, they display the decimal expansion of to the extent allowed. For example, with a modulo of 7, a calculator will show after the integer part and decimal point, if there is enough room:

0 00 (if fixed to 2 decimal places)
1 142857...
2 285714...
3 428571...
4 571428...
5 714285...
6 857142...

If the integer part of the result has as many digits as the calculator's screen has places, then the result will have the appearance of an integer, and is subject to rounding.

Some computer programming languages, and of course all computer algebra systems, offer built-in tools for modular arithmetic. Programming languages generally implement taking the remainder as an operator rather than a function. The percent sign (%) is used as a modulus operator in C++,[1] Javascript,[2] and a few others. In most flavors of BASIC, the word "mod" is used as the operator. Visual Basic used Mod[3] while REALbasic uses mod.[4]

"From a mathematical point of view, REALbasic's mod works incorrectly when the dividend is negative. Properly, the result of mod should always be positive," writes Matt Newburg.[5] Newburg then gives the examples (because is one more than ) but REALbasic says , as do "a number of other computer [programming] languages." Newburg then suggests programming one's own mod function:

 Function myMod(i as integer, k as integer) As integer
  return ((i mod k) + k) mod k
 End Function
  1. Harvey Deitel & Paul J. Deitel, C++: How to Program. Upper Saddle River, New Jersey: Prentice Hall (2009): p. 51
  2. David Flannagan, Javascript Pocket Reference Sebastopol, California: O'Reilly (2003): 10
  3. John Smiley, Learn to Program Visual Basic: Examples. Cincinnati: Muska & Lipman (2001) p. 18
  4. Matt Newburg, REALbasic: The Definitive Guide. Sebastopol, California: O'Reilly (2001): p. 170
  5. Newburg, ibid.