Sie sind auf Seite 1von 6

Fun With Modular Arithmetic

A reader recently suggested I write about modular arithmetic (aka taking the
remainder). I hadnt given it much thought, but realized the modulo is etremely
!ower"ul# it should be in our mental toolbo net to addition and multi!lication.
Instead o" hitting you in the "ace with "ormulas, lets e!lore an idea weve been subtly
e!osed to "or years. $heres a nice article on modular arithmetic that ins!ired this !ost.
Odd, Even and Threeven
%hortly a"ter discovering whole numbers (&, ', (, ), *+) we realized they "all into two
grou!s#
,ven# divisible by ' (-, ', ), ...)
/dd# not divisible by ' (&, (, *, 0+)
1hys this distinction im!ortant2 Its the beginning o" abstraction 3 were noticing the
properties o" a number (like being even or odd) and not 4ust the number itsel" ((05).
$his is huge 3 it lets us e!lore math at a dee!er level and "ind relationshi!s between
types o" numbers, not s!eci"ic ones. 6or eam!le, we can make rules like this#
,ven ,ven 7 ,ven
/dd /dd 7 /dd
,ven /dd 7 ,ven
$hese rules are general 3 they work at the !ro!erty level. (Intuitively, I have a
chemical analogy that evenness is a molecule some numbers have, and cannot be
removed by multi!lication.)
8ut even9odd is a very s!eci"ic !ro!erty# division by '. 1hat about the number (2 :ow
about this#
$hreeven means a number is divisbile by ( (-, (, ., ;+)
$hrodd means you are not divisible by ( (&, ', ), *, 0, <+)
1eird, but workable. =oull notice a "ew things# theres two ty!es o" throdd. A number
like )5 is & away "rom being threeven (remainder &), while the number * is two away
(remainder ').
8eing threeven is 4ust another !ro!erty o" a number. >erha!s not as immediately
use"ul as even9odd, but its there# we can make rules like threeven threeven 7
threeven and so on.
8ut its getting crazy. 1e cant make new words all the time.
Enter the Modulo
$he modulo o!eration (abbreviated mod, or ? in many !rogramming languages) is
the remainder when dividing. 6or eam!le, * mod ( 7 '5 which means ' is the
remainder when you divide * by (.
@onverting everyday terms to math, an even number is one where its - mod '5 3
that is, it has a remainder o" - when divided by '. An odd number is & mod '5 (has
remainder &).
1hys this cool2 1ell, our odd9even rules become this#
,ven ,ven 7 - - 7 - AevenB
/dd /dd 7 & & 7 & AoddB
,ven /dd 7 - & 7 - AevenB
@ool, huh2 >retty easy to work out 3 we converted !ro!erties into actual eCuations
and "ound some new "acts.
1hats even even odd odd2 1ell, its - - & & 7 -. In "act, you can see i"
theres an even being multi!lied anywhere the entire result is going to be zero+ I mean
even .
Clock Math
$he sneaky thing about modular math is weve already been using it "or kee!ing time
3 sometimes called clock arithmetic.
6or eam!le# its 0#-- (am9!m doesnt matter). 1here will the hour hand be in 0 hours2
:rm. 0 D 0 7 &), but we cant show &)#--5 on a clock. %o it must be '. 1e do this
reasoning intuitively, and in math terms#
(0 D 0) mod &' 7 (&)) mod &' 7 ' mod &' A' is the remainder when &) is
divided by &'B
$he eCuation &) mod &' 7 ' mod &'5 means, &) oclock and ' oclock look the
same on a &'Ehour clock. $hey are congruent, indicated by a tri!leEeCuals sign# &) F '
mod &'.
Another eam!le# its <#--. 1here will the big hand be in '* hours2
Instead o" adding '* to <, you might realize that '* hours is 4ust & day D & hour. %o,
the clock will end u! & hour ahead, at ;#--.
(< D '*) mod &' F (<) mod &' D ('*) mod &' F (<) mod &' D (&) mod &' F ;
mod &'
=ou intuitively converted '* to &, and added that to <.
Fun Property: Math jut !ork
Gsing clocks as an analogy, we can "igure out whether the rules o" modular arithmetic
4ust work (they do).
Addition"#u$traction
Hets say two times look the same on our clock ('#--5 and &)#--5). I" we add the
same hours to both, what ha!!ens2
1ell, they change to the same amount on the clockI '#-- D * hours F &)#-- D * hours
3 both will show 0#--.
1hy2 1ell, we never cared about the ecess &'#--5 that the &) was carrying around.
1e can 4ust add * to the ' remainder that both have, and they advance the same. 6or all
congruent numbers (' and &)), adding and subtracting has the same result.
Multiplication
Its harder to see whether multi!lication stays the same. I" &) F ' (mod &'), can we
multi!ly both sides and get the same result2
Hets see 3 what ha!!ens when we multi!ly by (2
1ell, '#-- J ( F .#--. 8ut whats &)#--5 J (2
Kemember, &) 7 &' D '. %o, we can say
&) J ( 7 (&' D ') J ( 7 (&' J () D (' J () mod &'
$he "irst !art (&' J () can be ignoredI $he &' hour over"low that &) is carrying
around 4ust gets re!eated a "ew times. 8ut who cares2 1e ignore the over"low anyway.
1hen multi!lying, its only the remainder that matters, which is the same ' hours "or
&)#-- and '#--. Intuitively, this is how I see that multi!lication doesnt change
relationshi!s with modular math (you can multi!ly both sides o" a modular relationshi!
and get the same result).
%e O& Modular Arithmetic
Low the "un !art 3 why is modular arithmetic use"ul2
#imple time calculation
1e do this intuitively, but its nice to give it a name. =ou have a "light arriving at (!m.
Its getting delayed &) hours. 1hat time will it land2
1ell, &) F ' mod &'. %o I think o" it as ' hours and an am9!m switch, so I know it
will be ( D ' 7 *am.
$his is a bit more involved than a !lain modulo o!erator, but the !rinci!le is the same.
Putting 'tem 'n (andom )roup
%u!!ose you have !eo!le who bought movie tickets, with a con"irmation number. =ou
want to divide them into ' grou!s.
1hat do you do2 /dds over here, evens over there. =ou dont need to know how
many tickets were issued ("irst hal", second hal"), everyone can "igure out their grou!
instantly (without contacting a central authority), and the scheme works as more !eo!le
buy tickets.
Leed ( grou!s2 Mivide by ( and take the remainder (aka mod (). =oull have grou!s
-5, &5 and '5.
In !rogramming, taking the modulo is how you can "it items into a hash table# i" your
table has L entries, convert the item key to a number, do mod L, and !ut the item in that
bucket (!erha!s kee!ing a linked list there). As your hash table grows in size, you can
recom!ute the modulo "or the keys.
Picking A (andom 'tem
I use the modulo in real li"e. Keally. 1e have ) !eo!le !laying a game and need to !ick
someone to go "irst. Play the mod N mini-game! Nive !eo!le numbers -, &, ', and (.
Low everyone goes one, two, three, shootI and !uts out a random number o" "ingers.
Add them u! and divide by ) 3 whoever gets the remainder eactly goes "irst. (6or
eam!le# i" the sum o" "ingers is &&, whoever had (5 gets to go "irst, since && mod ) 7
().
Its "ast and it works.
(unning Tak On A Cycle
%u!!ose tasks need to ha!!en on a certain schedule#
$ask A runs (9hour
$ask 8 runs .9hour
$ask @ runs &9hour
:ow do you store this in"ormation and make a schedule2 /ne way#
:ave a timer running every minute (kee! track o" the minute as n)
( 9 hour means once every .-9( 7 '- minutes. %o task A runs whenever n ?
'- 77 -5
$ask 8 runs whenever n ? &- 77 -5
$ask @ runs whenever n ? .- 77 -5
/h, you need task @& which runs & !er hour, but not the same time as task @2 %ure,
have it run when n mod .- 77 &5 (still once !er hour, but not the same as @&).
Oentally I see a cycle I want to hit at various intervals, so I insert a mod. $he neat
thing is that the hits can overla! inde!endently. Its a bit like P/K in that regard (each
P/K can be layered 3 but thats another articleI).
%imilarly, when !rogramming you can !rint every &--th log item by doing# i" (n ? &--
77 -)Q !rint+ R.
Its a very "leible, sim!le way to have items run on a schedule. In "act, its the way to
answer the 6izz8uzz sanity check. I" you dont have the modulo o!eration in your
batbelt the Cuestion becomes much more tricky.
Finding Propertie O& *um$er
%u!!ose I told you this#
a 7 ()0 J ' J ()
1hat can you deduce Cuickly2 1ell, a must be even, since its eCual to something
which involves multi!lication by '.
I" I also told you#
a 7 ((; J 0)
=oud balk. Lot because you know the two !roducts are di""erent, but because one is
clearly even, and the other is odd. $heres a !roblem# a cant be the same number in
both since the properties dont match up.
$hings like even, threeven and mod n are !ro!erties that are more general than
individual numbers, and which we can check "or consistency. %o we can use modulo to
"igure out whether numbers are consistent, without knowing what they areI
I" I tell you this#
(a D *b 7 <
(a D b 7 '
@an these eCuations be solved with the integers2 Hets see#
(a D *b 7 <+ lets mod ( it# - D 'b F ' mod (, or b F & mod (
(a D b 7 '+ lets mod ( it# - D b F ' mod (), or b F ' mod (
A contradication, good "ellowsI 8 cant be both & mod (5 and ' mod (5 3 its as
absurd as being even and odd at the same timeI
8ut theres one gotcha# numbers like &.*5 are neither even nor odd 3 they arent
integersI $he modular !ro!erties a!!ly to integers, so what we can say is that b cannot
be an integer.
8ecause, in "act, we can solve that eCuation#
((a D *b) S ((a Db) 7 < S '
)b 7 .
b 7 &.*
(a D &.* 7 ', so (a 7 -.*, and a 7 &9.
Mont get seduced by the !ower o" moduloI Tnow its limits# it a!!lies to integers.
Cryptography
>laying with numbers has very im!ortant uses in cry!togra!hy. Its too much to cover
here, but modulo is used in Mi""ieE:ellman Tey ,change 3 used in setting u! %%H
connections to encry!t web tra""ic.
Plain Englih
Neeks love to use technical words in regular contets. =ou might hear P is the same as
= modulo U which means roughly Ignoring U, P and = are the same.
6or eam!le#
b and 8 are identical, modulo ca!italization
$he i$ouch and i>ad are identical, modulo size
On!ard and %p!ard
Its strange thinking about the utility o" the modulo o!erator 3 its like someone
asking why e!onents are use"ul. In everyday li"e, not very, but its a tool to understand
!atterns in the world, and create your own.
In general, I see a "ew general use cases#
Kange reducer# take an in!ut, mod L, and you have a number "rom - to LE&.
Nrou! assigner# take an in!ut, mod L, and you have it tagged as a grou! "rom -
to LE&. $his grou! can be agreed u!on by any number o" !arties 3 "or eam!le,
di""erent servers that know L 7 '- can agree what grou! IM7*0 belongs to.
>ro!erty deducer# treat numbers according to !ro!erties (even, threeven, and so
on) and work out !rinci!les derived at the !ro!erty level
Im sure theres dozens more uses Ive missed 3 "eel "ree to comment below. :a!!y
mathI

Das könnte Ihnen auch gefallen