PrimePowerQ[expr]
yields True if expr is a power of a prime number, and yields False otherwise.
PrimePowerQ
PrimePowerQ[expr]
yields True if expr is a power of a prime number, and yields False otherwise.
Details and Options
- PrimePowerQ is typically used to test whether a number is a power of a prime number.
- A prime power is a prime or an integer power of a prime.
- PrimePowerQ[n] returns False unless n is manifestly a prime power.
- With the setting GaussianIntegers->True, PrimePowerQ tests whether a number is a Gaussian prime power.
- PrimePowerQ[m+In] automatically works over Gaussian integers.
Examples
open all close allBasic Examples (2)
Scope (5)
PrimePowerQ works over integers:
PrimePowerQ[9]PrimePowerQ[2 + I]PrimePowerQ[5, GaussianIntegers -> True]PrimePowerQ[1 / 25]PrimePowerQ[10 ^ 300 + 3]PrimePowerQ threads over lists:
PrimePowerQ[{1, 2, 3, 4, 5, 6, 7}]Applications (10)
Basic Applications (4)
Multicolumn[If[PrimePowerQ[#], Style[#, Red, Bold], #]& /@ Range[100], 10, ...]randomPrimePower[n_, m_ : 1] := RandomPrime[n, m] ^ RandomInteger[{1, 10}, m];randomPrimePower[10, 5]AllTrue[%, PrimePowerQ]ArrayPlot[Boole[Table[PrimePowerQ[a + b I], {a, 300}, {b, 300}]]]The first few prime powers that are not prime:
Select[Range[100], PrimePowerQ[#] && !PrimeQ[#]&]Number Theory (6)
Recognize Mersenne numbers, integers that have the form
:
mersenneQ[n_] := EvenQ[n + 1] && PrimePowerQ[n + 1];The number
is a Mersenne number;
is not:
mersenneQ[2147483647]mersenneQ[524285]The infinite sum of reciprocals of prime powers that are not prime converges:
Total[1. / Select[Range[10 ^ 5], PrimePowerQ[#] && !PrimeQ[#]&]]The number of elements in a finite field is a prime power:
PrimePowerQ[9]Compute the number of irreducible polynomials of degree n over a finite field of order q:
irreducibleCount[q_ ? PrimePowerQ, n_] := DivisorSum[n, MoebiusMu[#]q ^ (n / #)&] / n;Compute for degree 5 and order 9:
irreducibleCount[9, 5]The number of prime powers in intervals of size 1000:
Table[Count[Range[10 ^ i, 10 ^ i + 999], _ ? PrimePowerQ], {i, 0, 20}]A visualization of the growth of the prime powers:
ListLinePlot[Accumulate[%]]The distribution of prime powers over integers:
data = Select[Range[100], PrimePowerQ];𝒟 = EmpiricalDistribution[data];DiscretePlot[Evaluate[CDF[𝒟, x]], {x, 1, 100}, ExtentSize -> Right]The distribution of Gaussian prime powers:
data = {Re[#], Im[#]}& /@ Select[Flatten[Table[a + b I, {a, 500}, {b, 500}]], PrimePowerQ];𝒟 = EmpiricalDistribution[data];DiscretePlot3D[Evaluate[CDF[𝒟, {x, y}]], {x, 1, 5}, {y, 1, 5}, ExtentSize -> Right]Properties & Relations (11)
Prime powers are divisible by exactly one prime number:
PrimePowerQ[625]Select[Divisors[625], PrimeQ]The prime factorization of a prime power:
FactorInteger[125]Superscript@@@%PrimePowerQ gives True for all prime numbers:
PrimeQ[53]PrimePowerQ[53]The only square-free prime powers are prime numbers:
{PrimePowerQ[27], SquareFreeQ[27]}{PrimePowerQ[3], SquareFreeQ[3]}Use PrimeNu to count the number of distinct divisors:
PrimeNu[25]If PrimeNu returns 1, the number is a prime power:
PrimePowerQ[25]PrimeOmega gives the exponent for a prime power:
PrimePowerQ[7 ^ 5]PrimeOmega[7 ^ 5]MoebiusMu gives 0 for composite prime powers and
for primes:
{PrimePowerQ[121], CompositeQ[121], MoebiusMu[121]}{PrimePowerQ[11], PrimeQ[11], MoebiusMu[11]}Use FactorInteger to test for prime powers:
MatchQ[FactorInteger[2401], {{_Integer, _Integer}}]PrimePowerQ[2401]Use MangoldtLambda to test for a prime power:
MangoldtLambda[121] ≠ 0PrimePowerQ[121]Primes that are congruent to 1 mod 4 are not prime powers in the Gaussian integers:
Mod[29, 4]PrimePowerQ[29, GaussianIntegers -> True]The sum of divisors of a prime power n is less than 2n:
(DivisorSigma[1, #] < 2#) & /@ Select[Range[100], PrimePowerQ]Neat Examples (2)
Plot the prime powers that are the sum of three squares:
ArrayMesh[Boole[Table[PrimePowerQ[a ^ 2 + b ^ 2 + c ^ 2], {a, 10}, {b, 10}, {c, 10}]]]Plot the Ulam spiral, where numbers are colored based on whether they are a prime power:
ulam[n_] := Partition[Permute[Range[n ^ 2], Accumulate[Take[Flatten[{{n ^ 2 + 1} / 2, Table
[(-1) ^ j i, {j, n}, {i, {-1, n}}, {j}]}], n ^ 2]]], n];ArrayPlot[ulam[101]Boole[PrimePowerQ[ulam[101]]], ColorFunction -> "Rainbow", ColorRules -> {0 -> White}]Tech Notes
Related Guides
History
Text
Wolfram Research (2007), PrimePowerQ, Wolfram Language function, https://reference.wolfram.com/language/ref/PrimePowerQ.html.
CMS
Wolfram Language. 2007. "PrimePowerQ." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/PrimePowerQ.html.
APA
Wolfram Language. (2007). PrimePowerQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PrimePowerQ.html
BibTeX
@misc{reference.wolfram_2026_primepowerq, author="Wolfram Research", title="{PrimePowerQ}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/PrimePowerQ.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_primepowerq, organization={Wolfram Research}, title={PrimePowerQ}, year={2007}, url={https://reference.wolfram.com/language/ref/PrimePowerQ.html}, note=[Accessed: 13-June-2026]}