ArticleslgStudy

computer science

Shor's algorithm

Shor's algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Shor's algorithm rather than just read about it. In short: Shor's algorithm is a quantum algorithm for finding the prime factors of an integer. It was developed in 1994 by the American mathematician Peter Shor.

Shor's algorithm — main illustration
Shor's algorithm — illustration

Key takeaways

  • Shor's algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Shor's algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Shor's algorithm from memory before moving on to harder problems.

Reference excerpt

Shor's algorithm is a quantum algorithm for finding the prime factors of an integer. It was developed in 1994 by the American mathematician Peter Shor. It is one of the few known quantum algorithms with compelling potential applications and strong evidence of superpolynomial speedup compared to best known classical (non-quantum) algorithms. However, beating classical computers may require quantum computers with millions of qubits due to the overhead caused by quantum error correction. Shor proposed multiple similar algorithms for solving the factoring problem, the discrete logarithm problem, and the period-finding problem. "Shor's algorithm" usually refers to the factoring algorithm, but may refer to any of the three algorithms. The discrete logarithm algorithm and the factoring algorithm are instances of the period-finding algorithm, and all three are instances of the hidden subgroup problem. On a quantum computer, to factor an integer n, Shor's algorithm runs in polynomial time, meaning the time taken is polynomial in log⁡n. It takes quantum gates of order O((log⁡n)2(log⁡log⁡n)(log⁡log⁡log⁡n)) using fast multiplication, or even O((log⁡n)2(log⁡log⁡n)) using the asymptotically fastest multiplication algorithm currently known due to Harvey and van der Hoeven, thus demonstrating that the integer factorization problem is in complexity class BQP. Shor's algorithm is asymptotically faster than the most scalable classical factoring algorithm, the general number field sieve, which works in sub-exponential time.

Feasibility and implications

Assuming a quantum computer with a sufficient number of qubits could operate without succumbing to quantum noise and other quantum-decoherence phenomena, then Shor's algorithm could be used to break public-key cryptography schemes, such as

The RSA scheme The finite-field Diffie–Hellman key exchange The elliptic-curve Diffie–Hellman key exchange RSA can be broken if factoring large integers is computationally feasible. As far as is known, this is not possible using classical (non-quantum) computers; no classical algorithm is known that can factor integers in polynomial time. However, Shor's algorithm shows that factoring integers can be done with a polynomial complexity circuit on an ideal quantum computer. Thus, it might be feasible to defeat RSA by constructing a large enough quantum computer. This was a powerful motivator for the design and construction of quantum computers, and for the study of new quantum-computer algorithms. It has also facilitated research on new cryptosystems that are secure from quantum computers, collectively called post-quantum cryptography (PQC).

Physical implementation As of 2026, due to the high error rates of quantum computers and limited number of physical qubits available for quantum error correction, laboratory demonstrations of Shor's algorithm obtain correct results in only a fraction of attempts, and have only succeeded with small semiprimes. In 2001, Shor's algorithm was demonstrated by a group at IBM, who factored 15 into 3×5, using an NMR implementation of a quantum computer with seven qubits. After IBM's implementation, two independent groups implemented Shor's algorithm using photonic qubits. In 2012, the factorization of 15 was performed with solid-state qubits. Later, in 2012, the factorization of 21 was achieved. In 2016, the factorization of 15 was performed again using trapped-ion qubits. However, none of these demonstrations fulfill the requirements of Shor's algorithm: they compile the circuit using prior knowledge of the solution, and some have even oversimplified the algorithm in a way that makes it equivalent to coin flipping.

Algorithm The problem that we are trying to solve is: given an odd composite number N, find its integer factors. To achieve this, Shor's algorithm consists of two parts:

A classical reduction of the factoring problem to the problem of order-finding. This reduction is similar to that used for other factoring algorithms, such as the quadratic sieve. A quantum algorithm to solve the order-finding problem.

Classical reduction A complete factoring algorithm is possible if we're able to efficiently factor arbitrary N into just two integers p and q greater than 1, since if either p or q are not prime, then the factoring algorithm can in turn be run on those until only primes remain. A basic observation is that, using Euclid's algorithm, we can always compute the GCD between two integers efficiently. In particular, this means we can check efficiently whether N is even, in which case 2 is trivially a factor. Let us thus assume that N is odd for the remainder of this discussion. Afterwards, we can use efficient classical algorithms to check whether N is a prime power. For prime powers, efficient classical factorization algorithms exist, hence the rest of the quantum algorithm may assume that N is not a prime power. If those easy cases do not produce a nontrivial factor of N, the algorithm proceeds to handle the remaining case. We pick a random integer 2≤a<N. A possible nontrivial divisor of N can be found by computing gcd⁡(a,N), which can be done classically and efficiently using the Euclidean algorithm. If this produces a nontrivial factor (meaning gcd⁡(a,N)≠1), the algorithm is finished, and the other nontrivial factor is N/gcd⁡(a,N). If a nontrivial factor was not identified, then this means that N and the choice of a are coprime, so a is contained in the multiplicative group of integers modulo N, having a multiplicative inverse modulo N. Thus, a has a multiplicative order r modulo N, meaning

… excerpt ends here. Continue reading the full article.

Illustrations

Shor's algorithm: Quantum subroutine in Shor's algorithm
Quantum subroutine in Shor's algorithm

Worked examples

Example 1 — a first encounter with Shor's algorithm

Start with the simplest possible case. Write down what Shor's algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Shor's algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Shor's algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Shor's algorithm

In research
Shor's algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Shor's algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Shor's algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Integer factorization algorithms, Post-quantum cryptography, Quantum algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Shor's algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Shor's algorithm” →

How to study Shor's algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Shor's algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Shor's algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Shor's algorithm in simple terms?

Shor's algorithm is a quantum algorithm for finding the prime factors of an integer. It was developed in 1994 by the American mathematician Peter Shor.

Why does Shor's algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Shor's algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Shor's algorithm.

Tags

  • Integer factorization algorithms
  • Post-quantum cryptography
  • Quantum algorithms

Keep exploring