MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Phi

Function Phi

math/eulertotient.go:5–23  ·  view source on GitHub ↗

Phi is the Euler totient function. This function computes the number of numbers less then n that are coprime with n.

(n int64)

Source from the content-addressed store, hash-verified

3// Phi is the Euler totient function.
4// This function computes the number of numbers less then n that are coprime with n.
5func Phi(n int64) int64 {
6 result := n
7 for i := int64(2); i*i <= n; i += 1 {
8 if n%i == 0 {
9 for {
10 if n%i != 0 {
11 break
12 }
13 n /= i
14 }
15 result -= result / i
16 }
17 }
18
19 if n > 1 {
20 result -= result / n
21 }
22 return result
23}

Callers 2

TestPhiFunction · 0.85
BenchmarkPhiFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestPhiFunction · 0.68
BenchmarkPhiFunction · 0.68