Ma · Mathematics

How Many Integers Are Relatively Prime to a Given Number?

Published By the 9to5Science editorial team

At a glance

  1. 1. Find the prime factors

    Write the given integer as a product of distinct primes for the counting formula.

  2. 2. Apply inclusion–exclusion

    Subtract multiples of each prime, add back multiples of prime pairs, and continue alternating.

  3. 3. Count the interval

    For integers from 1 to n, use Euler’s totient formula; for another endpoint, use floors.

“Numbers of relatively primes” most commonly means: How many integers from 1 through a given positive integer nn have greatest common divisor 1 with nn? The answer is Euler’s totient function, written φ(n)\varphi(n). The same inclusion–exclusion idea also counts integers relatively prime to nn in a longer interval, such as 11 through NN.

Count integers from 1 through n

An integer aa is relatively prime to nn when gcd⁡(a,n)=1\gcd(a,n)=1. Factor nn into primes, keeping only the distinct primes:

n=p1e1p2e2⋯pkek.n=p_1^{e_1}p_2^{e_2}\cdots p_k^{e_k}.

A number from 1 through nn fails to be relatively prime to nn exactly when it is divisible by at least one of p1,…,pkp_1,\ldots,p_k. Inclusion–exclusion counts the numbers that avoid all of these prime divisors. The result is

φ(n)=n(1−1p1)(1−1p2)⋯(1−1pk).\varphi(n)=n\left(1-\frac{1}{p_1}\right)\left(1-\frac{1}{p_2}\right)\cdots\left(1-\frac{1}{p_k}\right).

Equivalently, multiply nn by one factor 1−1/p1-1/p for each distinct prime divisor pp of $n. The exponents in the prime factorization do not affect which primes must be excluded.

For n=1n=1, the usual convention is φ(1)=1\varphi(1)=1: the only integer in the range 1 through 1 is 1, and gcd⁡(1,1)=1\gcd(1,1)=1.

Worked example: count numbers relatively prime to 30

The distinct prime factors of 30 are 2, 3, and 5. Therefore,

φ(30)=30(1−12)(1−13)(1−15)=30⋅12⋅23⋅45=8.\begin{aligned} \varphi(30) &=30\left(1-\frac12\right)\left(1-\frac13\right)\left(1-\frac15\right)\\ &=30\cdot\frac12\cdot\frac23\cdot\frac45\\ &=8. \end{aligned}

So there are 8 integers from 1 through 30 relatively prime to 30. Listing them confirms the count:

1, 7, 11, 13, 17, 19, 23, 29.1,\ 7,\ 11,\ 13,\ 17,\ 19,\ 23,\ 29.

Every other integer in that range is divisible by 2, 3, or 5, so it shares a prime factor with 30.

Count up to a different endpoint

If you want the number of integers from 1 through NN relatively prime to nn, where NN is not necessarily nn, use inclusion–exclusion with floor functions. Let p1,…,pkp_1,\ldots,p_k be the distinct prime divisors of nn. For a positive integer dd, the number of multiples of dd from 1 through NN is ⌊N/d⌋\lfloor N/d\rfloor.

Start with all NN integers. Subtract the multiples of each prime divisor, add back the multiples of each pair of prime divisors, subtract the multiples of each triple, and continue with alternating signs. For example, if nn has three distinct prime divisors p,q,rp,q,r, the count is

N−⌊Np⌋−⌊Nq⌋−⌊Nr⌋+⌊Npq⌋+⌊Npr⌋+⌊Nqr⌋−⌊Npqr⌋.N-\left\lfloor\frac{N}{p}\right\rfloor-\left\lfloor\frac{N}{q}\right\rfloor-\left\lfloor\frac{N}{r}\right\rfloor +\left\lfloor\frac{N}{pq}\right\rfloor+\left\lfloor\frac{N}{pr}\right\rfloor+\left\lfloor\frac{N}{qr}\right\rfloor -\left\lfloor\frac{N}{pqr}\right\rfloor.

For n=30n=30 and N=100N=100, this gives

100−⌊1002⌋−⌊1003⌋−⌊1005⌋+⌊1006⌋+⌊10010⌋+⌊10015⌋−⌊10030⌋=100−50−33−20+16+10+6−3=26.\begin{aligned} 100-&\left\lfloor\frac{100}{2}\right\rfloor-\left\lfloor\frac{100}{3}\right\rfloor-\left\lfloor\frac{100}{5}\right\rfloor\\ &+\left\lfloor\frac{100}{6}\right\rfloor+\left\lfloor\frac{100}{10}\right\rfloor+\left\lfloor\frac{100}{15}\right\rfloor-\left\lfloor\frac{100}{30}\right\rfloor\\ &=100-50-33-20+16+10+6-3\\ &=26. \end{aligned}

Thus 26 integers from 1 through 100 are relatively prime to 30. The floor functions matter: for example, 33 integers in that range are divisible by 3, not 100/3 integers.

Why the formula works

For each prime divisor pp of nn, the fraction 1/p1/p of the integers in a full block of length nn are multiples of pp. Removing those integers for every prime factor would subtract some numbers more than once. Inclusion–exclusion corrects this: numbers divisible by two selected primes are added back, those divisible by three are subtracted again, and so on. In a full block of length nn, the resulting proportions multiply to ∏(1−1/p)\prod(1-1/p), yielding the totient formula.

The same reasoning works for any endpoint NN, but a partial block generally does not contain exact fractions such as N/pN/p of the multiples. That is why the general count uses floors.

Common mistakes

  • Using repeated prime factors: For n=12=22⋅3n=12=2^2\cdot3, use the primes 2 and 3 once each. The formula gives φ(12)=12(1−1/2)(1−1/3)=4\varphi(12)=12(1-1/2)(1-1/3)=4.
  • Counting only numbers less than nn without saying so: The standard definition counts 1≤a≤n1\leq a\leq n. For n>1n>1, including nn does not change the count, since gcd⁡(n,n)=n>1\gcd(n,n)=n>1.
  • Confusing “relatively prime to nn” with “prime”: A number can be composite and still be relatively prime to nn. For example, 49 is relatively prime to 30.
  • Using the totient formula for the wrong endpoint: φ(n)\varphi(n) counts candidates from 1 through nn. For a different endpoint, use inclusion–exclusion with floors.

References

  1. Introduction to Analytic Number Theory, Tom M. Apostol

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.