Concepts / Detecting Patterns in Sequences

Detecting Patterns in Sequences

A modulus result identifies a position within a repeating cycle. Because the results repeat through a fixed set of values, modulus can help detect patterns, check divisibility, and handle position-based problems.

  • Programming

The Leftover That Reveals a Pattern

Suppose 7 apples must be placed into groups of 3. Two complete groups can be made, and 1 apple remains. That leftover is the remainder. In Python, the modulus operator extracts this leftover value. It is written with the percent sign: a % b, where a is the dividend and b is the divisor.

Definition

The modulus operator (%) returns the remainder when one integer is divided by another.

7 applesitems to divide2 groupscomplete groups1 appleleftovergroups of 3left over
When 7 items are divided into groups of 3, how are the complete groups and the leftover represented?

Reading a Modulus Expression

In the expression a % b, a is the number being divided and b is the number that sets the group size. The result is the amount left after making as many complete groups of size b as possible. If a divides evenly by b, the result is 0. Otherwise, the result is the ungrouped remainder.

OperatorWhat it givesQuestion it answers
//The number of complete groupsHow many complete groups fit?
%The remainderWhat is left after making complete groups?
/The decimal resultWhat is the result of the division?

These operators examine the same division but return different information.

What do you think happens?

What remainder does 7 % 3 produce?

  • 1

  • 2

  • 3

Reveal answer
Answer:

1

Two complete groups of 3 use 6 items, leaving 1 item ungrouped.

Following the Repeating Cycle

Modulus becomes especially useful when the dividend increases through a sequence. With a divisor of 3, the remainders repeat as 0, 1, 2, 0, 1, 2, 0, 1, 2. The values never exceed 2, which is one less than the divisor. This predictable cycle lets a program recognize where a number falls within a repeating pattern.

00 % 311 % 322 % 303 % 314 % 325 % 3next dividendnext dividendcycle repeatsnext dividendnext dividend
As the dividend increases, how does the remainder repeat, and how does that reveal a cycle?
Key point

A modulus result identifies a position within a repeating cycle. Because the results repeat through a fixed set of values, modulus can help detect patterns, check divisibility, and handle position-based problems.

Predicting Different Remainders

Worked example

Evaluating Three Modulus Expressions

Predict the result of 8 % 3, 9 % 3, and 5 % 8.

  1. 1
    8 % 3

    Two complete groups of 3 use 6, leaving 2.

  2. 2
    9 % 3

    Three complete groups of 3 use all 9, so nothing is left.

  3. 3
    5 % 8

    The divisor is larger than the dividend, so no complete group of 8 can be made. The entire 5 remains.

Result

The results are 2, 0, and 5.

8 % 329 % 305 % 85
What remainder does each integer pair produce when evaluated with the modulus operator?

Patterns in Real Programs

The repeating behavior of modulus makes it useful whenever a program needs to recognize a cycle. It can detect even and odd numbers, check whether a number divides evenly, distribute items into buckets, and calculate positions in circular structures such as clocks or calendars.

Increasing number0, 1, 2, 3, ...Position 0remainder 0Position 1remainder 1Position 2remainder 2% 3% 3% 3
How can a changing number map to repeating positions such as weekdays, array indexes, or alternating states?
Best practice

Before using modulus, decide whether you need the number of complete groups or the leftover. Use floor division when you need complete groups, and use modulus when you need the remainder or a repeating position.

Mistakes to Avoid

  • Common mistake

    Treating % as ordinary division

    What it looks like

    Expecting 7 % 3 to describe the complete groups

    Why it fails

    Modulus returns what remains after complete groups are made, not the number of complete groups.

    Do this instead

    Use % for the remainder and // for the number of complete groups.

  • Common mistake

    Assuming a remainder must be nonzero

    What it looks like

    Expecting 9 % 3 to produce 3

    Why it fails

    When a number divides evenly, nothing is left over.

    Do this instead

    The modulus result is 0 when the division is exact.

  • Common mistake

    Assuming the result can be as large as the divisor

    What it looks like

    Expecting a result of 3 from an expression using divisor 3

    Why it fails

    The remainder stays between 0 and one less than the divisor.

    Do this instead

    For divisor 3, the possible repeating remainders are 0, 1, and 2.

  • Common mistake

    Assuming a smaller dividend becomes zero

    What it looks like

    Expecting 5 % 8 to be 0

    Why it fails

    No complete group of 8 fits inside 5, so all 5 remains.

    Do this instead

    When the dividend is smaller than the divisor, the dividend is the remainder.

Check Your Predictions

EASY

Predict the result of each expression before checking: 10 % 3, 12 % 4, 2 % 5, and 14 % 5. For each one, identify the complete groups and the leftover.

Hints
  • Ask how many complete groups of the divisor fit into the dividend.
  • The leftover is the modulus result.
  • If the dividend is smaller than the divisor, the dividend remains.
  • If the division is exact, the result is 0.

What do you think happens?

What results should the practice expressions produce?

Reveal answer
Answer:

10 % 3 is 1, 12 % 4 is 0, 2 % 5 is 2, and 14 % 5 is 4.

Each result is the amount left after making as many complete groups as possible.

Key Takeaways

  1. 1
    The modulus operator uses the syntax a % b and returns the remainder after integer division.
  2. 2
    Floor division gives the number of complete groups, while modulus gives what is left over.
  3. 3
    A modulus result is 0 when the division is exact and otherwise stays below the divisor.
  4. 4
    Applying the same modulus to increasing numbers creates a predictable repeating cycle.
  5. 5
    These cycles make modulus useful for divisibility checks, even and odd detection, buckets, and circular positions.

At a glance

  • The % operator returns the leftover from integer division.
  • In a % b, a is the dividend and b is the divisor.
  • The remainder is 0 for exact division and otherwise falls between 0 and b - 1.
  • Increasing dividends create repeating remainder cycles when the divisor stays the same.
  • Modulus helps programs recognize divisibility, repeating positions, and other patterns.

Related concepts

More concepts starting with D