Euler’s totient function counts the number of positive integers up to a given integer that are coprime to . In other word, the number of integers in the range for which the greatest common divisor . The function is written using the Greek letter phi as or and is often called Euler’s phi function.
An simple implementation of Euler’s totient function in Python is shown below:
import math
def phi(n):
= 0
amount
for i in range(1, n + 1):
if math.gcd(n, i) == 1:
+= 1
amount
return amount
Applications
One of the applications of Euler’s totient function is the RSA algorithm. Setting up a RSA system involves choosing large prime numbers and , computing and . Because and are both prime numbers, can quickly be calculated: .
Examples
If we have the product of two primes , then we can compute as