Hello! In our last lesson, we explored the integer factorization problem and its central role in RSA encryption, establishing why a fast factoring algorithm would be revolutionary. We saw that the security of much of the modern internet relies on the fact that factoring large numbers is incredibly difficult for classical computers.
This lesson bridges the gap between that classical problem and the quantum solution. We will unpack the first, crucial part of Shor's algorithm: a brilliant piece of number theory that transforms the hard problem of factoring into a different problem called period-finding. You will learn how finding the period of a specific mathematical function allows us to find the factors of the original number. This step is purely classical, but it's the key that unlocks the door for a quantum computer to step in and solve the problem efficiently.
1. The Core Idea: Turning Factoring into Period-Finding
Peter Shor's great insight was not just in designing a quantum algorithm, but in recognizing a mathematical structure within the factoring problem that was perfectly suited for a quantum computer to exploit. He showed that you could reduce factoring to finding the period of a modular exponentiation function.
Let's break down what this means.
First, to get a feel for this idea without heavy mathematics, let's read an excerpt from a blog post by Scott Aaronson, a prominent quantum computing researcher.
This article from the blog "Shtetl-Optimized" explains the conceptual leap from factoring to period-finding.
Read the beginning of the article, starting from "Alright, so let’s say you want to break the RSA cryptosystem..." down to the section that ends with "...maybe there’s some quantum operation we could perform on that superposition that would reveal the period." Focus on these points: Why "trying all divisors in parallel" isn't what quantum computers do. The concept of a periodic sequence using powers of a number x modulo N (e.g., powers of 2 mod 15). The high-level idea that finding the period of this sequence can tell us something about the factors of N.
As Aaronson explains, for a given number N that we want to factor, and a random guess a (where a < N and a shares no factors with N), we can look at the sequence:
This sequence of remainders will eventually repeat itself in a cycle. The length of this cycle is called the period, which we'll denote as . Finding this period is the key.
Let's see why this period is so important.
2. The "Magic Formula": From Period to Factors
Once you have the period , a few algebraic steps can lead you to the factors of N. The logic hinges on a few properties from number theory. The following video explains this connection very clearly.
Quantum Computing: Breaking Cryptography Using Shor's Factoring Algorithm
This clip from the "Advanced Maths" channel demonstrates the core mathematical trick that connects the period r to the factors of N.
Watch the segment on the key idea behind Shor's algorithm. This will walk you through the algebraic steps that use the period to find the factors.
Let's summarize the logic from the video.
-
The definition of the period tells us that . This is just another way of saying that gives a remainder of 1 when divided by
N. -
This means is a multiple of
N. We can write this as for some integerk. -
If the period is even, we can use the difference of squares formula () to rewrite the equation:
-
The Breakthrough: This final equation is the magic. It tells us that the number
N(whose factors we want to find) is a factor of the product .Unless we are unlucky (e.g., if is itself a multiple of
N), this implies that the factors ofNare "split" between the two terms, and . -
Finding the factors with GCD: We can then find a factor of
Nby calculating the Greatest Common Divisor (GCD) of one of these terms andN.factor_1 = gcd(a^{r/2} - 1, N)factor_2 = gcd(a^{r/2} + 1, N)
With high probability, these GCD calculations will yield the prime factors of N.
3. A Step-by-Step Example: Factoring 15
Let's make this concrete by factoring N = 15.
-
Pick a random guess. Let's pick
a = 7. We check thatgcd(7, 15) = 1, so it's a valid guess. -
Find the period
rof .- (since )
The sequence is7, 4, 13, 1, 7, 4, .... The sequence repeats after 4 terms, so the period is .
-
Check the conditions.
- Is
reven? Yes, 4 is even. - Is a multiple of
N? Let's check: .gcd(50, 15) = 5, not 15, so we are not in an "unlucky" case.
- Is
-
Calculate the factors using GCD.
-
factor_1 = gcd(a^{r/2} - 1, N) = gcd(7^2 - 1, 15) = gcd(48, 15)- The divisors of 48 are {1, 2, 3, 4, 6, 8, 12, 16, 24, 48}.
- The divisors of 15 are {1, 3, 5, 15}.
- The GCD is 3. We found a factor!
-
factor_2 = gcd(a^{r/2} + 1, N) = gcd(7^2 + 1, 15) = gcd(50, 15)- The divisors of 50 are {1, 2, 5, 10, 25, 50}.
- The divisors of 15 are {1, 3, 5, 15}.
- The GCD is 5. We found the other factor!
-
And indeed, . The method works.
The following video from the Qiskit team shows this exact process and how the "magic formula" is applied after finding the period.
Shor's Algorithm — Programming on Quantum Computers — Coding with Qiskit S2E7
This clip illustrates how the period r is plugged into the formulas to find the factors.
Watch these two short segments: The explanation of the "magic formula". Note that the video states p and q might not be the factors themselves, but will share cofactors with N (which is why we use GCD). The walk-through of the N=15 example, where a found period of r=4 is used to calculate the factors 3 and 5.
4. The Classical and Quantum Parts of Shor's Algorithm
At this point, you might be wondering: if we can do all this, why do we need a quantum computer?
The answer lies in Step 2: Find the period . For a large number N, the period r can also be very large. Calculating the sequence step-by-step until it repeats, as we did for N=15, is just as slow as trying to find the factors by brute force. It is a classically "hard" problem.
This is where the structure of Shor's algorithm becomes clear. It is a hybrid quantum-classical algorithm:

The Algorithm Outline:
- (Classical) Pick a random guess
a < N. Check ifgcd(a, N) > 1. If so, you're done by pure luck. - (Quantum) If not, use a quantum computer to run a period-finding algorithm on the function to find the period
r. This is the only quantum step. - (Classical) Check if
ris even and if . If these checks fail, go back to Step 1 with a new guessa. - (Classical) If the checks pass, calculate the factors using
gcd(a^{r/2} \pm 1, N).
The genius of Shor's algorithm is that it isolates the one classically hard part (period-finding) and outsources it to a quantum computer, which can solve that specific problem exponentially faster than any known classical algorithm.
Conclusion
In this lesson, you've learned the critical conceptual link that underpins Shor's algorithm. We have demystified the process, showing that it's not arbitrary magic but a clever application of number theory.
Key Takeaways:
- Shor's algorithm reduces the problem of factoring
Nto the problem of finding the periodrof the function . - Once the period
ris known, and provided it is even, the factors ofNcan be found efficiently on a classical computer using the Greatest Common Divisor (GCD) algorithm:gcd(a^{r/2} \pm 1, N). - The overall algorithm is a hybrid process. The quantum computer is used only for the period-finding step, which is computationally hard for classical computers. The rest of the logic is classical.
Preview of the Next Lesson:
We have now framed the exact problem that the quantum computer needs to solve: "Given the function , find its period r." In the next lesson, we will begin to explore the core quantum component that accomplishes this: the Quantum Fourier Transform (QFT). You will learn what the QFT is and how it is used to extract the period from a quantum superposition.