Primitive Root Calculator

Prime

Smallest primitive root mod 292
Number of primitive roots mod 2912
Factorization of p - 12^2 x 7
Order of the smallest root28
How this was calculated
  1. A primitive root modulo a prime p is an integer g whose powers run through every nonzero residue, so the order of g modulo p is the full p - 1 = 28.
  2. Factoring p - 1 gives 2^2 x 7; Euler's totient then counts phi(28) = 12 primitive roots in total.
  3. A candidate g fails if g^((p - 1)/q) = 1 mod p for some prime factor q of p - 1; testing 2, 3, 4, ... the first g that survives every prime factor is 2.
  4. So the smallest primitive root mod 29 is 2, and its powers cycle through all 28 nonzero residues before repeating.

A primitive root calculator tells you which numbers generate the full set of nonzero residues modulo a prime. A primitive root modulo a prime p is a number whose powers cycle through all p - 1 nonzero residues before returning to 1. Enter a prime and the calculator identifies the primitive roots, starting with the smallest one. For example, the smallest primitive root mod 29 is 2, and there are phi(28) = 12 primitive roots mod 29. The smallest primitive root mod 23 is 5, while 2, 3 and 4 all fail because their orders divide 22. The smallest primitive root mod 43 is 3, with phi(42) = 12 primitive roots in total.

The calculator uses the standard test built on the prime factors of p - 1: a candidate g fails if g^((p - 1)/q) is 1 mod p for some prime factor q of p - 1. This is the accepted method for deciding whether a number is a primitive root, because it checks the order of g directly against the factors that could make it smaller than p - 1. On this page you can see the full formula, the step-by-step workings for each candidate, worked examples such as the ones above, and the sources behind the method, so you can follow every result rather than take it on trust.

Worked examples

Primitive roots modulo 29

(29)

Smallest primitive root mod 292
Number of primitive roots mod 2912
Factorization of p - 12^2 x 7
Order of the smallest root28

Primitive roots modulo 23

(23)

Smallest primitive root mod 235
Number of primitive roots mod 2310
Factorization of p - 12 x 11
Order of the smallest root22

Primitive roots modulo 43

(43)

Smallest primitive root mod 433
Number of primitive roots mod 4312
Factorization of p - 12 x 3 x 7
Order of the smallest root42

What is a primitive root?

A primitive root modulo a prime p is a number whose powers cycle through all p - 1 nonzero residues. In other words, if you take a primitive root g and compute g, g², g³, and so on up to g^(p - 1), the results run through every nonzero remainder modulo p exactly once before returning to 1. No nonzero residue is skipped and none repeats early. This is the property a primitive root calculator checks for you, and it is what makes such a number special among the p - 1 possible nonzero residues.

To state this precisely, we need the idea of order. The order of a number g modulo p is the smallest positive exponent k such that g^k ≡ 1 (mod p). Every nonzero residue has some order, and that order always divides p - 1. A primitive root is simply a residue whose order equals the full p - 1. When the order is smaller, the powers of g cycle through only a subset of the residues and then repeat, so g cannot reach everything. Because a primitive root has order p - 1, its powers generate the entire multiplicative group modulo p: every nonzero residue can be written as some power of g. That is why the nonzero residues modulo a prime form a cyclic group, and why a primitive root is also called a generator.

Concrete examples make the distinction clear. Modulo 29, the smallest primitive root is 2, and there are phi(28) = 12 primitive roots in total. Modulo 43, the smallest primitive root is 3, with phi(42) = 12 primitive roots in total. Not every small number works, though. Modulo 23, the smallest primitive root is 5; 2, 3 and 4 all fail because their orders divide 22. Each of those numbers cycles through only part of the nonzero residues before repeating, so none of them generates the whole group.

Checking a candidate by hand would mean computing up to p - 1 powers, which gets tedious quickly. There is a faster route: testing a candidate g only needs the prime factors of p - 1. Specifically, g fails if g^((p - 1)/q) is 1 mod p for some prime factor q of p - 1. If that test comes back as something other than 1 for every prime factor q, then g is a primitive root. This shortcut is exactly what a primitive root calculator applies, which is why it can return an answer for large primes without listing every power.

A primitive root modulo a prime p has order exactly p - 1, so its powers cover all p - 1 nonzero residues before repeating.

Worked examples

A primitive root modulo a prime p is a number whose powers cycle through all p − 1 nonzero residues. The worked examples below show how a primitive root calculator gets its answers: factor p − 1 into primes, then test each candidate g using only those prime factors. A candidate fails if g^((p − 1)/q) is 1 mod p for some prime factor q of p − 1. If no such q produces 1, the candidate is a primitive root.

For p = 29, p − 1 = 28, whose prime factors are 2 and 7. The candidate 2 passes both tests, so the smallest primitive root mod 29 is 2. The count of primitive roots is phi(28) = 12, meaning 12 of the 28 nonzero residues mod 29 are primitive roots.

For p = 23, p − 1 = 22, whose prime factors are 2 and 11. Here the smallest primitive root mod 23 is 5. The candidates 2, 3 and 4 all fail because their orders divide 22 — each one collapses to 1 at one of the two required exponents, so none of them cycles through all 22 nonzero residues.

For p = 43, p − 1 = 42, whose prime factors are 2, 3 and 7. The smallest primitive root mod 43 is 3, and the total number of primitive roots is phi(42) = 12. Running these three cases through a primitive root calculator follows the same short path every time: factor p − 1, test the candidate against each prime factor, and count the survivors with Euler's totient.

  • p = 29: p − 1 = 28 = 2² × 7; smallest primitive root is 2; phi(28) = 12 primitive roots.
  • p = 23: p − 1 = 22 = 2 × 11; smallest primitive root is 5; 2, 3 and 4 fail because their orders divide 22.
  • p = 43: p − 1 = 42 = 2 × 3 × 7; smallest primitive root is 3; phi(42) = 12 primitive roots.
Prime pp − 1 and its prime factorsSmallest primitive rootPrimitive roots in total
2928 = 2² × 72phi(28) = 12
2322 = 2 × 115—
4342 = 2 × 3 × 73phi(42) = 12
The test never needs every exponent — only the prime factors of p − 1. One q with g^((p − 1)/q) ≡ 1 mod p is enough to rule a candidate out.

How the candidate test works

A primitive root modulo a prime p is a number whose powers cycle through all p - 1 nonzero residues. That means the order of the number — the smallest positive exponent k for which g^k is 1 mod p — has to equal exactly p - 1. The candidate test used by a primitive root calculator rests on one fact from group theory: the order of any element always divides p - 1. So if you can rule out every proper divisor of p - 1 as a possible order, the only option left is p - 1 itself, and the candidate is a primitive root.

You do not have to check every divisor one at a time. Every proper divisor of p - 1 divides at least one number of the form (p - 1)/q, where q is a prime factor of p - 1. If the order of g were some proper divisor d, then d would divide (p - 1)/q for at least one prime factor q, and g raised to (p - 1)/q would come out to 1 mod p. Turning that around gives the test: g fails if g^((p - 1)/q) is 1 mod p for some prime factor q. If none of those powers equals 1, no proper divisor can be the order, so the order must be the full p - 1.

The arithmetic behind this is simple to see. Suppose p - 1 factors as a product of primes with exponents, and write d for the order of g. If d is smaller than p - 1, then at least one prime q divides p - 1 more times than it divides d, which forces d to divide (p - 1)/q. That is the only gap the test needs to probe, and each prime factor of p - 1 supplies one probe. The number of checks therefore equals the number of distinct prime factors of p - 1, not the size of p itself — which is why the primitive root calculator stays fast even when p is large.

A worked example makes the rule concrete. Modulo 23, p - 1 = 22 = 2 × 11, so a candidate must be tested at the exponents 22/2 = 11 and 22/11 = 2. The smallest primitive root mod 23 is 5. The numbers 2, 3 and 4 all fail because their orders divide 22 — they are caught by one of those two probes, so none of them cycles through all 22 nonzero residues. Modulo 29, p - 1 = 28 = 2^2 × 7, giving probes at 28/2 = 14 and 28/7 = 4; the smallest primitive root is 2, and there are phi(28) = 12 primitive roots mod 29 in total. Modulo 43, p - 1 = 42 = 2 × 3 × 7, so the probes sit at 21, 14 and 6; the smallest primitive root is 3, with phi(42) = 12 primitive roots in total.

Two practical notes follow from the test. First, only the distinct prime factors of p - 1 matter — repeated factors such as the 2^2 in 28 add no extra probe, because the single exponent 28/2 already covers them. Second, a candidate that passes every probe is proven to be a primitive root, not merely suspected of being one; there is no probabilistic guesswork in the check itself.

The test needs one exponentiation per distinct prime factor of p - 1. If g^((p - 1)/q) is not 1 mod p for every such q, the order of g cannot be any proper divisor of p - 1, so it must be p - 1.

Where primitive roots matter

Primitive roots are not just a textbook curiosity. They are the engine behind some of the most widely used public-key cryptography on the internet. Diffie-Hellman key exchange and other discrete-logarithm protocols are built on primitive roots of large primes, and when you run a primitive root calculator you are working with the same structure those systems depend on.

The connection is direct. A primitive root modulo a prime p is a number whose powers cycle through all p - 1 nonzero residues. That means every nonzero residue mod p can be written as a power of g. In a discrete-logarithm protocol, two parties agree on a large prime p and a primitive root g, then each raises g to a secret exponent. The shared secret comes from the fact that powers of g reach every nonzero residue, so the arithmetic stays inside a complete, well-behaved set. Anyone listening sees g raised to some power, but recovering the exponent from that value is the discrete logarithm problem, which is believed to be hard for large primes.

The same rule that makes a primitive root calculator useful is what makes these protocols workable. Testing a candidate g only needs the prime factors of p - 1: g fails if g^((p - 1)/q) is 1 mod p for some prime factor q. So verifying a generator is a matter of checking a handful of exponentiations rather than testing every power. A calculator applies exactly this test, which is why it can confirm a primitive root for a large prime without listing the entire cycle.

Small examples make the structure easy to see. The smallest primitive root mod 29 is 2, and there are phi(28) = 12 primitive roots mod 29. The smallest primitive root mod 23 is 5; 2, 3 and 4 all fail because their orders divide 22. The smallest primitive root mod 43 is 3, with phi(42) = 12 primitive roots in total. In each case the count of primitive roots is phi(p - 1), so a large prime has many valid generators to choose from — a useful property when a protocol needs to pick one.

That abundance matters in practice. Because there are phi(p - 1) primitive roots, a system is not forced to use a single fixed generator; it can select among many. The primitive root calculator's job is to confirm which candidates qualify, using the prime factors of p - 1 as its checklist. The same check that validates a small example like 2 mod 29 scales to the large primes used in key exchange.

A primitive root's powers cover every nonzero residue mod p — the completeness that discrete-logarithm protocols rely on.

FAQs

What is a primitive root?

A primitive root is a number g whose powers mod p produce every nonzero residue before repeating. In other words, its order is p - 1, the largest possible order modulo p. Because the powers of g cycle through all the values from 1 to p - 1 exactly once before returning to the start, g generates the entire multiplicative group of nonzero residues modulo p.

What is the smallest primitive root of 29?

The smallest primitive root of 29 is 2. There are 12 primitive roots mod 29 in total, so 2 is only the first of them in numeric order. The other primitive roots are the powers 2^k mod 29 where k shares no factor with 28, which is why exactly 12 of them exist.

Why is 2 not a primitive root of 23?

Because 2^11 is congruent to 1 mod 23, the order of 2 divides 11 instead of being the full 22. A primitive root of 23 must have order 22, so 2 falls short. The smallest primitive root of 23 is 5, which does reach the full order of 22.

How many primitive roots does a prime p have?

A prime p has exactly phi(p - 1) primitive roots, where phi is Euler's totient function. The totient counts the integers up to p - 1 that share no factor with p - 1, and each corresponds to one primitive root. For example, mod 29 there are 12 primitive roots and mod 43 there are also 12.

Does every prime have a primitive root?

Yes, every prime has a primitive root. In fact, every prime p has exactly phi(p - 1) of them, where phi is Euler's totient function. So the count is never zero: each prime has at least one primitive root, and the exact number depends only on the value of p - 1.

What is the smallest primitive root of 43?

The smallest primitive root of 43 is 3. There are 12 primitive roots mod 43 in total, so 3 is just the first of them in numeric order. The remaining primitive roots are the powers 3^k mod 43 where k shares no factor with 42, which is why exactly 12 exist.

Sources

Related calculators

Modular Inverse Calculator
Find the modular multiplicative inverse with the extended Euclidean algorithm, or confirm that none exists.
Carmichael Function Calculator
The Carmichael function lambda(n), the smallest universal exponent modulo n, from the prime factorization, compared with Euler's totient.
Euler's Totient Calculator
Euler's totient φ(n) — how many integers up to n are coprime to n — from the distinct prime factors of n.