coprimes
number theory
mathematics
Euler's totient function
prime numbers

How many numbers below N are coprimes to N?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In mathematics, particularly number theory, one often encounters the concept of coprime numbers. Two integers are said to be coprime, or relatively prime, if they share no common positive divisors other than 1. When examining integers below a certain number, NN, a common query arises: how many of these integers are coprime to NN? This question is answered using a well-established mathematical function known as Euler's Totient Function, denoted as ϕ(N)\phi(N).

Euler's Totient Function

Euler’s Totient Function, ϕ(N)\phi(N), gives the count of integers up to NN that are coprime to NN. For example, if N=10N = 10, the numbers 1, 3, 7, and 9 are coprime to 10, so ϕ(10)=4\phi(10) = 4.

The Mathematical Formula

If NN is expressed as a product of distinct prime factors, p1,p2,,pkp_1, p_2, \ldots, p_k, the totient function can be determined through the formula:

ϕ(N)=N(11p_1)(11p_2)(11p_k)\phi(N) = N \left(1 - \frac{1}{p\_1}\right)\left(1 - \frac{1}{p\_2}\right)\cdots\left(1 - \frac{1}{p\_k}\right)

Where the prime factors pip_i are distinct prime numbers dividing NN.

Example Calculation

Let's find ϕ(36)\phi(36):

  1. Prime Factorization: First, find the prime factors of 36: 36=22×3236 = 2^2 \times 3^2.
  2. Apply the Formula:

ϕ(36)=36(112)(113)=361223=3613=12\phi(36) = 36 \left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{3}\right) = 36 \cdot \frac{1}{2} \cdot \frac{2}{3} = 36 \cdot \frac{1}{3} = 12

Thus, there are 12 numbers less than 36 that are coprime to 36.

Properties of Euler's Totient Function

Multiplicative Nature: If mm and nn are coprime, then ϕ(mn)=ϕ(m)ϕ(n)\phi(mn) = \phi(m)\phi(n). • For Prime Numbers: If pp is a prime, ϕ(p)=p1\phi(p) = p - 1 because every number less than pp is coprime to pp. • For Powers of a Prime: If pp is a prime and kk is a positive integer, ϕ(pk)=pkpk1=pk(11p)\phi(p^k) = p^k - p^{k-1} = p^k\left(1 - \frac{1}{p}\right).

Practical Applications

Euler's Totient function is pivotal in several domains:

Cryptography: It's used in the RSA algorithm for generating public and private keys. In RSA, computing ϕ(N)\phi(N) where NN is a product of two large primes is the basis for determining the encryption and decryption keys.

Combinatorial Counting: The function counts the number of residue classes modulo a number that are invertible, which is crucial in various enumeration problems in number theory.

Example Table of Totient Function Values

Here's a small table to summarize Euler's Totient function values for integers from 1 to 10:

NNPrime Factorsϕ(N)\phi(N)Coprime Numbers
1-11
2211
3321, 2
4222^221, 3
5541, 2, 3, 4
62, 321, 5
7761, 2, 3, 4, 5, 6
8232^341, 3, 5, 7
9323^261, 2, 4, 5, 7, 8
102, 541, 3, 7, 9

Conclusion

Euler's Totient function serves as a bridge into the intricacies of number theory, deepening our understanding of properties like coprimality and modular arithmetic. It embodies the elegance of mathematical abstraction while also offering tangible applications in fields such as cryptography. Understanding the distribution and nature of numbers in relation to their coprimes empowers mathematicians and computer scientists alike to construct solutions to complex problems both theoretical and practical.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.