How Many Integers Are Relatively Prime to a Given Number?
At a glance
- 1. Find the prime factors
Write the given integer as a product of distinct primes for the counting formula.
- 2. Apply inclusion–exclusion
Subtract multiples of each prime, add back multiples of prime pairs, and continue alternating.
- 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 have greatest common divisor 1 with ? The answer is Euler’s totient function, written . The same inclusion–exclusion idea also counts integers relatively prime to in a longer interval, such as through .
Count integers from 1 through n
An integer is relatively prime to when . Factor into primes, keeping only the distinct primes:
A number from 1 through fails to be relatively prime to exactly when it is divisible by at least one of . Inclusion–exclusion counts the numbers that avoid all of these prime divisors. The result is
Equivalently, multiply by one factor for each distinct prime divisor of $n. The exponents in the prime factorization do not affect which primes must be excluded.
For , the usual convention is : the only integer in the range 1 through 1 is 1, and .
Worked example: count numbers relatively prime to 30
The distinct prime factors of 30 are 2, 3, and 5. Therefore,
So there are 8 integers from 1 through 30 relatively prime to 30. Listing them confirms the count:
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 relatively prime to , where is not necessarily , use inclusion–exclusion with floor functions. Let be the distinct prime divisors of . For a positive integer , the number of multiples of from 1 through is .
Start with all 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 has three distinct prime divisors , the count is
For and , this gives
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 of , the fraction of the integers in a full block of length are multiples of . 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 , the resulting proportions multiply to , yielding the totient formula.
The same reasoning works for any endpoint , but a partial block generally does not contain exact fractions such as of the multiples. That is why the general count uses floors.
Common mistakes
- Using repeated prime factors: For , use the primes 2 and 3 once each. The formula gives .
- Counting only numbers less than without saying so: The standard definition counts . For , including does not change the count, since .
- Confusing “relatively prime to ” with “prime”: A number can be composite and still be relatively prime to . For example, 49 is relatively prime to 30.
- Using the totient formula for the wrong endpoint: counts candidates from 1 through . For a different endpoint, use inclusion–exclusion with floors.
References
- 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.