linkedin github twitter

Understanding Modulo (remainder of division)

% is {{m}}, because...
If you have {{l}} coins,
and want to buy items that cost {{r}} coins each,
you could only buy {{wholes}} items,
meaning you would only spend {{wholes*r}} coins ({{wholes}}x{{r}}),
and still have {{m}} coins left, the remainder ({{l}}-{{wholes*r}}).
If you have {{l}} coins,
and want to buy items that cost {{r}} coins each,
you could buy exactly {{wholes}} items,
but then you would have {{m}} coins left
If you prefere, you could also think of it as fractions:
{{l}}
{{r}}
is the same as
{{parseInt(l/r)}}
{{l%r}}
{{r}}