# eulerPhi

Euler phi function

## Syntax

``p = eulerPhi(n)``

## Description

example

````p = eulerPhi(n)` evaluates the Euler phi function or (also known as the totient function) for a positive integer `n`.```

## Examples

collapse all

Compute the Euler phi function $\varphi \left(n\right)$ for the integer $n=35$.

`p = eulerPhi(35)`
```p = 24 ```

The Euler phi function satisfies the multiplicative property $\varphi \left(xy\right)=\varphi \left(x\right)\phantom{\rule{0.2777777777777778em}{0ex}}\varphi \left(y\right)$ if the two integers $x$ and $y$ are relatively prime (also known as coprime). The integer factorization of 35 is 7 and 5, which are relatively prime. Show that $\varphi \left(35\right)$ satisfies the multiplicative property.

Calculate $\varphi \left(x\right)$ and $\varphi \left(y\right)$ for the two factors.

`px = eulerPhi(7)`
```px = 6 ```
`py = eulerPhi(5)`
```py = 4 ```

Verify that `px` and `py` satisfy the multiplicative property.

`p = px*py`
```p = 24 ```

If a positive integer $n$ has prime factorization $n={p}_{1}^{{k}_{1}}\phantom{\rule{0.2777777777777778em}{0ex}}{p}_{2}^{{k}_{2}}\phantom{\rule{0.2777777777777778em}{0ex}}\dots \phantom{\rule{0.2777777777777778em}{0ex}}{p}_{r}^{{k}_{r}}$ with distinct prime factors ${p}_{1},{p}_{2},\dots ,{p}_{r}$, then the Euler phi function satisfies the product formula

$\varphi \left(n\right)=n\left(1-\frac{1}{{p}_{1}}\right)\left(1-\frac{1}{{p}_{2}}\right)\dots \left(1-\frac{1}{{p}_{r}}\right)$.

The integer $n=36$ has distinct prime factors $2$ and $3$. Show that $\varphi \left(36\right)$ satisfies the Euler's product formula.

Declare $36$ as a symbolic number and evaluate $\varphi \left(36\right)$.

`n = sym(36)`
`n = $36$`
`p = eulerPhi(n)`
`p = $12$`

List the prime factors of $36$.

`f_n = factor(n)`
`f_n = $\left(\begin{array}{cccc}2& 2& 3& 3\end{array}\right)$`

Substitute the prime factors $2$ and $3$ into the product formula.

`p_product = n*(1-1/2)*(1-1/3)`
`p_product = $12$`

Euler's theorem states that ${a}^{\varphi \left(n\right)}\equiv 1\left(mod\phantom{\rule{0.2777777777777778em}{0ex}}n\right)$ if and only if the two positive integers $a$ and $n$ are relatively prime. Show that the Euler phi function $\varphi \left(n\right)$ satisfies Euler's theorem for the integers $a=15$ and $n=4$.

```a = 15; n = 4; isCongruent = powermod(a,eulerPhi(n),n) == mod(1,n)```
```isCongruent = logical 1 ```

Confirm that `a` and n are relatively prime.

`g = gcd(a,n)`
```g = 1 ```

Calculate the Euler phi function $\varphi \left(n\right)$ for the integers $n$ from 1 to 1000.

`P = eulerPhi(1:1000);`

Find the mean value of $\varphi \left(n\right)/n$.

`Pave = mean(P./(1:1000))`
```Pave = 0.6082 ```

## Input Arguments

collapse all

Input, specified as a number, vector, matrix, array, symbolic number, or symbolic array. The elements of `n` must be positive integers.

Data Types: `single` | `double` | `sym`

collapse all

### Euler Phi Function

The Euler phi function $\varphi \left(n\right)$ computes the number of integers between 1 and n that are relatively prime (also known as coprime) to n. Two integers are relatively prime if there is no integer greater than one that divides them both. In other words, their greatest common divisor is one.

## References

[1] Redmond, D. Number Theory: An Introduction to Pure and Applied Mathematics. New York: Marcel Dekker, 1996.