Here’s a visually engaging number theory problem that explores Fermat’s Little Theorem through cyclic patterns in modular exponentiation.
Problem: Visualizing Cyclic Patterns in Modular Exponentiation
Fermat’s Little Theorem states that if $( p )$ is a prime number and $( a )$ is an integer not divisible by $( p )$, then:
$$
a^{p-1} \equiv 1 \pmod{p}
$$
This theorem implies that for each $( a )$, the sequence of powers $( a^1, a^2, \dots, a^{p-1} )$ modulo $( p )$ will eventually repeat, forming a cyclic pattern.
In this example, we will:
- Choose a modulus $( p )$, a prime number (e.g., $23$).
- Compute $( a^k \mod p )$ for each integer $( a )$ from $1$ to $( p-1 )$ and for $( k )$ from $1$ to $( p-1 )$.
- Plot these powers to observe any patterns and cycles.
Approach
We’ll calculate $( a^k \mod p )$ for each value of $( a )$ from $1$ to $( p-1 )$ and plot the results as points, where the x-axis represents the base $( a )$, the y-axis represents the result of $( a^k \mod p )$, and different colors show different powers $( k )$.
Python Code
Here’s the $Python$ code to find and plot the cyclic patterns:
1 | import matplotlib.pyplot as plt |
Explanation of the Code
- Modular Exponentiation Calculation:
- For each integer $( a )$ from $1$ to $( p-1 )$, we compute $( a^k \mod p )$ for $( k = 1 )$ to $( p-1 )$.
- These results reveal the pattern of powers under modulo $( p )$, showing the periodic behavior predicted by Fermat’s Little Theorem.
- Plotting:
- Each point $(a, a^k \mod p)$ represents the result of raising $( a )$ to power $( k )$ modulo $( p )$.
- The color of each point indicates the power $( k )$, which helps in visualizing different cycles.
Visualization

The scatter plot shows:
- Cyclic Behavior: Each base $( a )$ follows a distinct cycle when raised to powers $( k )$ modulo $( p )$.
- Color Coding: Different colors represent different exponents $( k )$, making it easy to observe how the results cycle back, eventually repeating after certain intervals.
Interpretation
- Cyclic Patterns: This visualization shows how powers of numbers “wrap around” in modular arithmetic, creating cycles based on Fermat’s Little Theorem.
- Applications: Modular exponentiation is central to cryptography, particularly in $RSA$ and Diffie-Hellman key exchange, where large powers modulo a prime number are computed.
- Visual Insight: Observing the cyclic patterns provides an intuitive grasp of Fermat’s Little Theorem and the repeating nature of modular exponentiation.
This graph effectively brings out the cyclic beauty in modular exponentiation and demonstrates the predictability and structure found in number theory.








