Math Fundamentals18 sections · 814 units
Open in Course

Pattern - Cyclic Behavior

Wrapping around

Modulo creates cycles. The values of nmod5n \bmod 5 repeat in the pattern 0,1,2,3,4,0,1,2,3,4,...0, 1, 2, 3, 4, 0, 1, 2, 3, 4, ... as nn increases.

This is perfect for circular arrays or rotating through options. If you have an array of length nn and want to access index ii where ii might exceed nn, use imodni \bmod n.

For example, days of the week cycle every 77 days. If today is day 33 (Wednesday) and you want to know the day 100100 days from now, compute (3+100)mod7=5(3 + 100) \bmod 7 = 5 (Friday).