Euler came up with this function more than 200 years back. At that time this function drew less attention, indeed it had no much value as there is no direct application other than in number theory. However, today this function plays a major role in cyber security applications.

Euler’s Totient Function, also known as Phi Function, Prime Counting Function or is used to count number of co-primes prior to a given integer. In other words, the function outputs number of co-primes which are less than the given number.

Co-primes are integers which does not share any common factors. For an example even though 8 is not a prime, it is a co-prime of 15. Because 8 = 2*2*2 and 15 = 2*5, therefore it can be seen that 8 or 15 does not share a common factor.

If a given integer is a prime, obviously all the integers less than the given number are co-primes to the given prime. Therefore, it is easy to calculate if n is a prime. When *n* is a prime; in

When* n* is not a prime, can be calculated as follow:

First, the number should be broken into prime factors, then after it is a matter of applying the following equation:

*(prime factors of n)*

### Multiplicative Property

This is another interesting property of which can be used to compute .

When, p and q are prime numbers:

as a fact, if p and q are prime numbers then,

### Example

Calculating :

Using the above equation:

n = 24, m = 2 and primes are 2, 3:

Will check if the answer is correct, using the besic definition:

Following are all the integers less than 24, starting from 1

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23

Remove co-prime of 24:

**1**, ~~2, 3, 4~~, **5**, ~~6~~, **7**, ~~8, 9, 10~~, **11**, ~~12~~, **13**, ~~14, 15, 16~~, **17**, ~~18~~, **19**, ~~20, 21, 22~~, **23**

Remaining number count is 8, therefore both answers match.

### Python Implementation

It is possible to use the above equation to calculate in Python. However, it is easier to use the definition of prime counting rather than using the equation.

The algorithm would be as follow:

- needs to be calculated.
- Start from
*k*= 2,*counter*is 0. - Check if the selected integer (
*k*) is a co-prime to*n*. - If it is a co-prime increase the counter.

To check if *k* is a co-prime to *n*, GCD operation can be used.

def calculate_phi(n): phi_n = 1 count = 0 i = 2 while (i<n): r = gcd_euc(n, i) i = i + 1 if(r==1): phi_n = phi_n + 1 return phi_n

### Summary

Above mentioned methods can be used to calculate of a given integer. Results of the phi function is often applied in cryptography.