Let . In the Shor algorithm, first use classical arithmetic to deal with even and perfect powers, and to reject prime inputs. Perfect-power recognition and primality testing have polynomial-time classical algorithms. Factoring a perfect power , with , already gives a nontrivial factor . We may therefore consider an odd composite with at least two distinct prime factors.
Choose uniformly from and compute its greatest common divisor with using the Euclidean algorithm. A proper nontrivial divisor ends the computation. Otherwise is a unit, and the remaining task is quantum order finding. Set
Prepare a uniform quantum superposition in a -qubit register and use quantum modular exponentiation in a second register to obtain
Repeated squaring and reversible modular multiplication implement this step with polynomially many quantum gates in . Apply the inverse quantum Fourier transform to the first register and perform a quantum measurement in the computational basis, obtaining . Measuring the second register first, and Fourier-transforming the resulting periodic coset, gives the same distribution of .
Here is a quantitative quantum Fourier sampling bound for order finding. If , multiplication by cycles its orbit states. The Fourier eigenstates of that cycle have eigenvalues , and the initial state has squared overlap with each, for . Therefore
Conditioned on a particular eigenphase , its nearest integer outcome obeys and has probability at least . Indeed the absolute amplitude is , where . For , the numerator is at least and the denominator at most ; the limiting value at is one.
If , the fraction is reduced, and gives
Use continued-fraction recovery in quantum order finding. The relevant approximation theorem says that reduced fractions satisfying are continued fraction convergents of . For an input rational of digits, their candidates can be enumerated classically in time, and their denominators are at most the reduced input denominator. This is the supplied continued-fraction result; discard candidate denominators exceeding . A zero measurement is treated as a failed order-finding sample.
For each remaining even candidate denominator , compute by repeated squaring, and try and . Return only a divisor with ; otherwise repeat the quantum sample or choose another . This candidate-denominator gcd post-processing cannot return an incorrect factor, since its output is checked directly. When the recovered denominator is the true even multiplicative order and the square root of one is nontrivial, part (i) guarantees success. Checking before trying the gcds is also a valid, more restrictive convention; the single-run distinction matters in part (iii).
Two further standard quantitative facts explain why repetitions remain efficient. First, for a uniformly chosen unit modulo an odd integer with at least two distinct prime divisors, the probability of a useful unit in Shor factorization is at least : its true multiplicative order is even and . To see the source of this bound, the Chinese remainder theorem for unit groups makes the prime-power components independent. Each odd-prime-power unit group is cyclic. If its 2-primary order is , the probability of the largest possible value of is , and the probability of any specified smaller value is at most . Failure requires all components to have the same : either all zero, making odd, or all the same positive value, making the halfway power in every component. Conditioning on one component, the chance another component matches it is at most , so total failure is at most .
Second, the fraction of phase numerators coprime to is , where is the Euler totient function. The elementary totient-ratio lower bound suffices: if the distinct prime divisors are , then and . The totient product formula therefore gives
Thus for a good , the probability of sampling a coprime phase numerator with the required accuracy is at least . This is inverse-polynomial in the input length, so polynomially many repetitions achieve any fixed success probability. The quantum Fourier transform uses elementary rotations and Hadamard gates in the ideal gate description, and the other quantum and classical stages are polynomial in .
The reduction is therefore polynomial in the bit length:
A small denominator need not itself be the true multiplicative order; verification of the final factor is essential, and no guarantee is claimed for every individual sample.
An Euler pseudoprime is an odd composite number with satisfying , where the right side is the Jacobi symbol. A weaker convention requires only ; it is distinguished below. A strong pseudoprime, with and odd, satisfies or for some .
By the Chinese remainder theorem for unit groups, the unit group modulo is . Modulo , . Thus the only possible sign in is positive. Modulo , writing for a primitive root, the condition is , equivalently . Consequently has multiplicative order dividing in each factor. Its square is either or in each factor, but these signs must agree: modulo , the Legendre symbol is for square and for square , and the same is true modulo . The Euler pseudoprime condition additionally requires the product of those Legendre symbols to be . Hence
Each sign has two roots modulo each prime, hence four roots modulo . There are eight pseudoprime bases modulo under the Jacobi symbol convention. Under the weaker sign-only convention there are instead sixteen pseudoprime bases, since the two signs of the squares need not agree; the stated equivalence therefore specifically uses the Jacobi symbol convention.
For the strong pseudoprime condition, . Since the unit group has exponent of a finite group , the only possibilities are or . The six pseudoprime bases are . For example , while has but . Thus the strong-pseudoprime pseudoprime bases are not a subgroup.
An odd composite number with is a Fermat pseudoprime to base when
For , the Chinese remainder theorem for unit groups reduces this condition to congruences modulo and . Modulo , Fermat's little theorem gives
so exactly when . Modulo , the unit group has order , so
The equation in the cyclic group has solutions, namely .
Combining the two independent sign choices by the Chinese remainder theorem gives
and hence
Put , so and . The primes and are ramified:
Thus the finite modulus in the question is
The ray class exact sequence, with no real component in the modulus, gives
The Chinese remainder theorem for unit groups and the ramification relations yield
The image of kills the coming from . Consequently the kernel of the map from the ray class group to the ordinary class group is
Since the given class number is two, .
It remains to distinguish from . Let
The prime splits in , and represents the nontrivial ordinary ideal class because no element of has norm . Direct multiplication, or comparison of norms and valuations at the two primes over , gives
Modulo , the element is . Modulo , its class is nontrivial and has order three because . Hence the ray class of has order six: its square is a nontrivial element of order three in the congruence kernel. The remaining order-two factor of that kernel, coming from , is independent of . Therefore
The Lagrange root bound over a field says that a nonzero polynomial of degree over a field has at most roots. In particular, a polynomial modular congruence of degree modulo a prime number has at most incongruent solutions unless all its coefficients vanish modulo that prime.
Suppose that is good and that the positive integer divisor divides . The roots of form a finite multiplicative subgroup of . By the fact that every finite multiplicative subgroup of a field is cyclic, is a cyclic group of order . The solutions in of are
so there are at least of them. The Lagrange root bound over a field gives at most roots in all of , hence exactly . Thus every divisor of a good number is good.
Now put . By the Chinese remainder theorem for unit groups, a base is a Fermat-pseudoprime base precisely when its two components satisfy
The power roots in a finite field and
show that each component has ten choices. Therefore there are
Fermat-pseudoprime bases.
To impose the strong pseudoprime condition, write with odd. Since , we have . In each cyclic group of ten Fermat components, raising to the th power sends five elements to and five elements to . A pair of components passes the strong test exactly when their signs agree: the pair satisfies the first alternative, and satisfies the second at . Components of opposite sign become after squaring and can never jointly equal . Hence the number of strong-pseudoprime bases is
For an odd integer having at least two distinct prime divisors, choose a uniform unit . The Chinese remainder theorem for unit groups makes its odd-prime-power components independent cyclic groups. Failure of factor extraction from an even modular order requires all components to have equal 2-primary order exponents. For any component each possible exponent has probability at most , so conditioning on another component bounds failure by . Excluding the identity unit only improves the success proportion.
Let for distinct odd prime numbers with . Exactly unit bases make a Fermat pseudoprime, and exactly of these make it a strong pseudoprime. The Fermat bases form under the Chinese remainder theorem for unit groups. If , then and is odd, so the th-power map sends five elements of each to and five to . The strong condition requires equal signs in both components, giving .