📜  欧拉的Totient函数

📅  最后修改于: 2021-04-30 03:29:08             🧑  作者: Mango

输入n的欧拉Totient函数Φ(n)是{1、2、3,…,n}中相对于n素数的数字的计数,即,n的GCD(最大公约数)为1的数字。
例子 :

Φ(1) = 1  
gcd(1, 1) is 1

Φ(2) = 1
gcd(1, 2) is 1, but gcd(2, 2) is 2.

Φ(3) = 2
gcd(1, 3) is 1 and gcd(2, 3) is 1

Φ(4) = 2
gcd(1, 4) is 1 and gcd(3, 4) is 1

Φ(5) = 4
gcd(1, 5) is 1, gcd(2, 5) is 1, 
gcd(3, 5) is 1 and gcd(4, 5) is 1

Φ(6) = 2
gcd(1, 6) is 1 and gcd(5, 6) is 1, 

如何为输入nΦ计算Φ(n)
一种简单的解决方案是遍历从1到n-1的所有数字,并使用n为1的gcd对数字进行计数。下面是实现输入整数n的欧拉Totient函数的简单方法的实现。

C++
// A simple C++ program to calculate
// Euler's Totient Function
#include 
using namespace std;
 
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// A simple method to evaluate Euler Totient Function
int phi(unsigned int n)
{
    unsigned int result = 1;
    for (int i = 2; i < n; i++)
        if (gcd(i, n) == 1)
            result++;
    return result;
}
 
// Driver program to test above function
int main()
{
    int n;
    for (n = 1; n <= 10; n++)
        cout << "phi("<


C
// A simple C program to calculate Euler's Totient Function
#include 
 
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// A simple method to evaluate Euler Totient Function
int phi(unsigned int n)
{
    unsigned int result = 1;
    for (int i = 2; i < n; i++)
        if (gcd(i, n) == 1)
            result++;
    return result;
}
 
// Driver program to test above function
int main()
{
    int n;
    for (n = 1; n <= 10; n++)
        printf("phi(%d) = %d\n", n, phi(n));
    return 0;
}


Java
// A simple java program to calculate
// Euler's Totient Function
import java.io.*;
 
class GFG {
 
    // Function to return GCD of a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
 
    // A simple method to evaluate
    // Euler Totient Function
    static int phi(int n)
    {
        int result = 1;
        for (int i = 2; i < n; i++)
            if (gcd(i, n) == 1)
                result++;
        return result;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n;
 
        for (n = 1; n <= 10; n++)
            System.out.println("phi(" + n + ") = " + phi(n));
    }
}
 
// This code is contributed by sunnusingh


Python3
# A simple Python3 program
# to calculate Euler's
# Totient Function
 
# Function to return
# gcd of a and b
def gcd(a, b):
 
    if (a == 0):
        return b
    return gcd(b % a, a)
 
# A simple method to evaluate
# Euler Totient Function
def phi(n):
 
    result = 1
    for i in range(2, n):
        if (gcd(i, n) == 1):
            result+=1
    return result
 
# Driver Code
for n in range(1, 11):
    print("phi(",n,") = ",
           phi(n), sep = "")
            
# This code is contributed
# by Smitha


C#
// A simple C# program to calculate
// Euler's Totient Function
using System;
 
class GFG {
 
    // Function to return GCD of a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
 
    // A simple method to evaluate
    // Euler Totient Function
    static int phi(int n)
    {
        int result = 1;
        for (int i = 2; i < n; i++)
            if (gcd(i, n) == 1)
                result++;
        return result;
    }
 
    // Driver code
    public static void Main()
    {
        for (int n = 1; n <= 10; n++)
        Console.WriteLine("phi(" + n + ") = " + phi(n));
    }
}
 
// This code is contributed by nitin mittal


PHP
<Φphp
// PHP program to calculate
// Euler's Totient Function
 
// Function to return
// gcd of a and b
function gcd($a, $b)
{
    if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}
 
// A simple method to evaluate
// Euler Totient Function
function phi($n)
{
    $result = 1;
    for ($i = 2; $i < $n; $i++)
        if (gcd($i, $n) == 1)
            $result++;
    return $result;
}
 
// Driver Code
for ($n = 1; $n <= 10; $n++)
    echo "phi(" .$n. ") =" . phi($n)."\n";
 
// This code is contributed by Sam007
Φ>


Javascript


C++
// C++ program to calculate Euler's
// Totient Function using Euler's
// product formula
#include 
using namespace std;
 
int phi(int n)
{
     
    // Initialize result as n
    float result = n;
  
    // Consider all prime factors of n
    // and for every prime factor p,
    // multiply result with (1 - 1/p)
    for(int p = 2; p * p <= n; ++p)
    {
         
        // Check if p is a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then update n and result
            while (n % p == 0)
                n /= p;
                 
            result *= (1.0 - (1.0 / (float)p));
        }
    }
  
    // If n has a prime factor greater than sqrt(n)
    // (There can be at-most one such prime factor)
    if (n > 1)
        result *= (1.0 - (1.0 / (float)n));
  
    return (int)result;
}
  
// Driver code
int main()
{
    int n;
     
    for(n = 1; n <= 10; n++)
    {
        cout << "Phi" << "("
             << n << ")" << " = "
             << phi(n) <


C
// C program to calculate Euler's Totient Function
// using Euler's product formula
#include 
 
int phi(int n)
{
    float result = n; // Initialize result as n
 
    // Consider all prime factors of n and for every prime
    // factor p, multiply result with (1 - 1/p)
    for (int p = 2; p * p <= n; ++p) {
         
        // Check if p is a prime factor.
        if (n % p == 0) {
             
            // If yes, then update n and result
            while (n % p == 0)
                n /= p;
            result *= (1.0 - (1.0 / (float)p));
        }
    }
 
    // If n has a prime factor greater than sqrt(n)
    // (There can be at-most one such prime factor)
    if (n > 1)
        result *= (1.0 - (1.0 / (float)n));
 
    return (int)result;
}
 
// Driver program to test above function
int main()
{
    int n;
    for (n = 1; n <= 10; n++)
        printf("phi(%d) = %d\n", n, phi(n));
    return 0;
}


Java
// Java program to calculate Euler's Totient
// Function using Euler's product formula
import java.io.*;
 
class GFG {
    static int phi(int n)
    {
        // Initialize result as n
        float result = n;
 
        // Consider all prime factors of n and for
        // every prime factor p, multiply result
        // with (1 - 1/p)
        for (int p = 2; p * p <= n; ++p) {
            // Check if p is a prime factor.
            if (n % p == 0) {
                // If yes, then update n and result
                while (n % p == 0)
                    n /= p;
                result *= (1.0 - (1.0 / (float)p));
            }
        }
 
        // If n has a prime factor greater than sqrt(n)
        // (There can be at-most one such prime factor)
        if (n > 1)
            result *= (1.0 - (1.0 / (float)n));
 
        return (int)result;
    }
 
    // Driver program to test above function
    public static void main(String args[])
    {
        int n;
        for (n = 1; n <= 10; n++)
            System.out.println("phi(" + n + ") = " + phi(n));
    }
}
 
// This code is contributed by Nikita Tiwari.


Python3
# Python 3 program to calculate
# Euler's Totient Function
# using Euler's product formula
 
def phi(n) :
 
    result = n   # Initialize result as n
      
    # Consider all prime factors
    # of n and for every prime
    # factor p, multiply result with (1 - 1 / p)
    p = 2
    while p * p<= n :
 
        # Check if p is a prime factor.
        if n % p == 0 :
 
            # If yes, then update n and result
            while n % p == 0 :
                n = n // p
            result = result * (1.0 - (1.0 / float(p)))
        p = p + 1
         
         
    # If n has a prime factor
    # greater than sqrt(n)
    # (There can be at-most one
    # such prime factor)
    if n > 1 :
        result = result * (1.0 - (1.0 / float(n)))
  
    return int(result)
     
     
# Driver program to test above function
for n in range(1, 11) :
    print("phi(", n, ") = ", phi(n))
    
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# program to calculate Euler's Totient
// Function using Euler's product formula
using System;
 
class GFG {
     
    static int phi(int n)
    {
         
        // Initialize result as n
        float result = n;
 
        // Consider all prime factors
        // of n and for every prime
        // factor p, multiply result
        // with (1 - 1 / p)
        for (int p = 2; p * p <= n; ++p)
        {
             
            // Check if p is a prime factor.
            if (n % p == 0)
            {
                 
                // If yes, then update
                // n and result
                while (n % p == 0)
                    n /= p;
                result *= (float)(1.0 - (1.0 / (float)p));
            }
        }
 
        // If n has a prime factor
        // greater than sqrt(n)
        // (There can be at-most
        // one such prime factor)
        if (n > 1)
            result *= (float)(1.0 - (1.0 / (float)n));
 
        return (int)result;
    }
 
    // Driver Code
    public static void Main()
    {
        int n;
        for (n = 1; n <= 10; n++)
            Console.WriteLine("phi(" + n + ") = " + phi(n));
    }
}
 
// This code is contributed by nitin mittal.


PHP
<Φphp
// PHP program to calculate
// Euler's Totient Function
// using Euler's product formula
function phi($n)
{
    // Initialize result as n
    $result = $n;
 
    // Consider all prime factors
    // of n and for every prime
    // factor p, multiply result
    // with (1 - 1/p)
    for ($p = 2; $p * $p <= $n; ++$p)
    {
         
        // Check if p is
        // a prime factor.
        if ($n % $p == 0)
        {
             
            // If yes, then update
            // n and result
            while ($n % $p == 0)
                $n /= $p;
            $result *= (1.0 - (1.0 / $p));
        }
    }
 
    // If n has a prime factor greater
    // than sqrt(n) (There can be at-most
    // one such prime factor)
    if ($n > 1)
        $result *= (1.0 - (1.0 / $n));
 
    return intval($result);
}
 
// Driver Code
for ($n = 1; $n <= 10; $n++)
echo "phi(" .$n. ") =" . phi($n)."\n";
     
// This code is contributed by Sam007
Φ>


Javascript
// Javascript program to calculate
// Euler's Totient Function
// using Euler's product formula
function phi(n)
{
    // Initialize result as n
    let result = n;
 
    // Consider all prime factors
    // of n and for every prime
    // factor p, multiply result
    // with (1 - 1/p)
    for (let p = 2; p * p <= n; ++p)
    {
         
        // Check if p is
        // a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then update
            // n and result
            while (n % p == 0)
                n /= p;
            result *= (1.0 - (1.0 / p));
        }
    }
 
    // If n has a prime factor greater
    // than sqrt(n) (There can be at-most
    // one such prime factor)
    if (n > 1)
        result *= (1.0 - (1.0 / n));
 
    return parseInt(result);
}
 
// Driver Code
for (let n = 1; n <= 10; n++)
 document.write(`phi(${n}) = ${phi(n)} 
`);       // This code is contributed by _saurabh_jaiswal


C++
// C++ program to calculate Euler's
// Totient Function
#include 
using namespace std;
 
int phi(int n)
{
    // Initialize result as n
    int result = n;
  
    // Consider all prime factors of n
    // and subtract their multiples
    // from result
    for(int p = 2; p * p <= n; ++p)
    {
         
        // Check if p is a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then update n and result
            while (n % p == 0)
                n /= p;
                 
            result -= result / p;
        }
    }
  
    // If n has a prime factor greater than sqrt(n)
    // (There can be at-most one such prime factor)
    if (n > 1)
        result -= result / n;
         
    return result;
}
  
// Driver code
int main()
{
    int n;
    for(n = 1; n <= 10; n++)
    {
        cout << "Phi" << "("
             << n << ")" << " = "
             << phi(n) << endl;
    }
    return 0;
}
 
// This code is contributed by koulick_sadhu


C
// C program to calculate Euler's Totient Function
#include 
 
int phi(int n)
{
    int result = n; // Initialize result as n
 
    // Consider all prime factors of n and subtract their
    // multiples from result
    for (int p = 2; p * p <= n; ++p) {
         
        // Check if p is a prime factor.
        if (n % p == 0) {
             
            // If yes, then update n and result
            while (n % p == 0)
                n /= p;
            result -= result / p;
        }
    }
 
    // If n has a prime factor greater than sqrt(n)
    // (There can be at-most one such prime factor)
    if (n > 1)
        result -= result / n;
    return result;
}
 
// Driver program to test above function
int main()
{
    int n;
    for (n = 1; n <= 10; n++)
        printf("phi(%d) = %d\n", n, phi(n));
    return 0;
}


Java
// Java program to calculate
// Euler's Totient Function
import java.io.*;
 
class GFG
{
static int phi(int n)
{
    // Initialize result as n
    int result = n;
 
    // Consider all prime factors
    // of n and subtract their
    // multiples from result
    for (int p = 2; p * p <= n; ++p)
    {
         
        // Check if p is
        // a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then update
            // n and result
            while (n % p == 0)
                n /= p;
            result -= result / p;
        }
    }
 
    // If n has a prime factor
    // greater than sqrt(n)
    // (There can be at-most
    // one such prime factor)
    if (n > 1)
        result -= result / n;
    return result;
}
 
// Driver Code
public static void main (String[] args)
{
    int n;
    for (n = 1; n <= 10; n++)
        System.out.println("phi(" + n +
                           ") = " + phi(n));
}
}
 
// This code is contributed by ajit


Python3
# Python3 program to calculate
# Euler's Totient Function
def phi(n):
     
    # Initialize result as n
    result = n;
 
    # Consider all prime factors
    # of n and subtract their
    # multiples from result
    p = 2;
    while(p * p <= n):
         
        # Check if p is a
        # prime factor.
        if (n % p == 0):
             
            # If yes, then
            # update n and result
            while (n % p == 0):
                n = int(n / p);
            result -= int(result / p);
        p += 1;
 
    # If n has a prime factor
    # greater than sqrt(n)
    # (There can be at-most
    # one such prime factor)
    if (n > 1):
        result -= int(result / n);
    return result;
 
# Driver Code
for n in range(1, 11):
    print("phi(",n,") =", phi(n));
     
# This code is contributed
# by mits


C#
// C# program to calculate
// Euler's Totient Function
using System;
 
class GFG
{
     
static int phi(int n)
{
// Initialize result as n
int result = n;
 
// Consider all prime 
// factors of n and
// subtract their
// multiples from result
for (int p = 2;
         p * p <= n; ++p)
{
     
    // Check if p is
    // a prime factor.
    if (n % p == 0)
    {
         
        // If yes, then update
        // n and result
        while (n % p == 0)
            n /= p;
        result -= result / p;
    }
}
 
// If n has a prime factor
// greater than sqrt(n)
// (There can be at-most
// one such prime factor)
if (n > 1)
    result -= result / n;
return result;
}
 
// Driver Code
static public void Main ()
{
    int n;
    for (n = 1; n <= 10; n++)
        Console.WriteLine("phi(" + n +
                              ") = " +
                              phi(n));
}
}
 
// This code is contributed
// by akt_mit


PHP
<Φphp
// PHP program to calculate
// Euler's Totient Function
 
function phi($n)
{
    // Initialize
    // result as n
    $result = $n;
 
    // Consider all prime
    // factors of n and subtract
    // their multiples from result
    for ($p = 2;
         $p * $p <= $n; ++$p)
    {
         
        // Check if p is
        // a prime factor.
        if ($n % $p == 0)
        {
             
            // If yes, then
            // update n and result
            while ($n % $p == 0)
                $n = (int)$n / $p;
            $result -= (int)$result / $p;
        }
    }
 
    // If n has a prime factor
    // greater than sqrt(n)
    // (There can be at-most
    // one such prime factor)
    if ($n > 1)
        $result -= (int)$result / $n;
    return $result;
}
 
// Driver Code
for ($n = 1; $n <= 10; $n++)
    echo "phi(", $n,") =",
          phi($n), "\n";
     
// This code is contributed
// by ajit
Φ>


Javascript
// Javascript program to calculate
// Euler's Totient Function
 
function phi(n)
{
    // Initialize
    // result as n
    let result = n;
 
    // Consider all prime
    // factors of n and subtract
    // their multiples from result
    for (let p = 2;
         p * p <= n; ++p)
    {
         
        // Check if p is
        // a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then
            // update n and result
            while (n % p == 0)
                n = parseInt(n / p);
            result -= parseInt(result / p);
        }
    }
 
    // If n has a prime factor
    // greater than sqrt(n)
    // (There can be at-most
    // one such prime factor)
    if (n > 1)
        result -= parseInt(result / n);
    return result;
}
 
// Driver Code
for (let n = 1; n <= 10; n++)
    document.write(`phi(${n}) = ${phi(n)} 
`);       // This code is contributed // by _saurabh_jaiswal


输出 :

phi(1) = 1
phi(2) = 1
phi(3) = 2
phi(4) = 2
phi(5) = 4
phi(6) = 2
phi(7) = 6
phi(8) = 4 
phi(9) = 6
phi(10) = 4

上面的代码调用gcd函数O(n)次。 gcd函数的时间复杂度为O(h),其中“ h”是在给定的两个数较小的情况下的位数。因此,上述解决方案的时间复杂度的上限是O(nLogn)[从1到n的所有数字中最多最多可以记录10个n位的Log]
下面是一个更好的解决方案。这个想法是基于Euler的乘积公式,该公式指出,上位函数的值低于n的乘积整体素因数p。

欧乐产品

该公式基本上说,对于n的所有素数p,Φ(n)的值等于n乘以(1/1 / p)的乘积。例如Φ(6)= 6 *(1-1 / 2)*(1 – 1/3)= 2。
我们可以使用本文中使用的想法找到所有主要因素。

1) Initialize : result = n
2) Run a loop from 'p' = 2 to sqrt(n), do following for every 'p'.
     a) If p divides n, then 
           Set: result = result  * (1.0 - (1.0 / (float) p));
           Divide all occurrences of p in n.
3) Return result  

以下是欧拉产品公式的实现。

C++

// C++ program to calculate Euler's
// Totient Function using Euler's
// product formula
#include 
using namespace std;
 
int phi(int n)
{
     
    // Initialize result as n
    float result = n;
  
    // Consider all prime factors of n
    // and for every prime factor p,
    // multiply result with (1 - 1/p)
    for(int p = 2; p * p <= n; ++p)
    {
         
        // Check if p is a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then update n and result
            while (n % p == 0)
                n /= p;
                 
            result *= (1.0 - (1.0 / (float)p));
        }
    }
  
    // If n has a prime factor greater than sqrt(n)
    // (There can be at-most one such prime factor)
    if (n > 1)
        result *= (1.0 - (1.0 / (float)n));
  
    return (int)result;
}
  
// Driver code
int main()
{
    int n;
     
    for(n = 1; n <= 10; n++)
    {
        cout << "Phi" << "("
             << n << ")" << " = "
             << phi(n) <

C

// C program to calculate Euler's Totient Function
// using Euler's product formula
#include 
 
int phi(int n)
{
    float result = n; // Initialize result as n
 
    // Consider all prime factors of n and for every prime
    // factor p, multiply result with (1 - 1/p)
    for (int p = 2; p * p <= n; ++p) {
         
        // Check if p is a prime factor.
        if (n % p == 0) {
             
            // If yes, then update n and result
            while (n % p == 0)
                n /= p;
            result *= (1.0 - (1.0 / (float)p));
        }
    }
 
    // If n has a prime factor greater than sqrt(n)
    // (There can be at-most one such prime factor)
    if (n > 1)
        result *= (1.0 - (1.0 / (float)n));
 
    return (int)result;
}
 
// Driver program to test above function
int main()
{
    int n;
    for (n = 1; n <= 10; n++)
        printf("phi(%d) = %d\n", n, phi(n));
    return 0;
}

Java

// Java program to calculate Euler's Totient
// Function using Euler's product formula
import java.io.*;
 
class GFG {
    static int phi(int n)
    {
        // Initialize result as n
        float result = n;
 
        // Consider all prime factors of n and for
        // every prime factor p, multiply result
        // with (1 - 1/p)
        for (int p = 2; p * p <= n; ++p) {
            // Check if p is a prime factor.
            if (n % p == 0) {
                // If yes, then update n and result
                while (n % p == 0)
                    n /= p;
                result *= (1.0 - (1.0 / (float)p));
            }
        }
 
        // If n has a prime factor greater than sqrt(n)
        // (There can be at-most one such prime factor)
        if (n > 1)
            result *= (1.0 - (1.0 / (float)n));
 
        return (int)result;
    }
 
    // Driver program to test above function
    public static void main(String args[])
    {
        int n;
        for (n = 1; n <= 10; n++)
            System.out.println("phi(" + n + ") = " + phi(n));
    }
}
 
// This code is contributed by Nikita Tiwari.

Python3

# Python 3 program to calculate
# Euler's Totient Function
# using Euler's product formula
 
def phi(n) :
 
    result = n   # Initialize result as n
      
    # Consider all prime factors
    # of n and for every prime
    # factor p, multiply result with (1 - 1 / p)
    p = 2
    while p * p<= n :
 
        # Check if p is a prime factor.
        if n % p == 0 :
 
            # If yes, then update n and result
            while n % p == 0 :
                n = n // p
            result = result * (1.0 - (1.0 / float(p)))
        p = p + 1
         
         
    # If n has a prime factor
    # greater than sqrt(n)
    # (There can be at-most one
    # such prime factor)
    if n > 1 :
        result = result * (1.0 - (1.0 / float(n)))
  
    return int(result)
     
     
# Driver program to test above function
for n in range(1, 11) :
    print("phi(", n, ") = ", phi(n))
    
 
# This code is contributed
# by Nikita Tiwari.

C#

// C# program to calculate Euler's Totient
// Function using Euler's product formula
using System;
 
class GFG {
     
    static int phi(int n)
    {
         
        // Initialize result as n
        float result = n;
 
        // Consider all prime factors
        // of n and for every prime
        // factor p, multiply result
        // with (1 - 1 / p)
        for (int p = 2; p * p <= n; ++p)
        {
             
            // Check if p is a prime factor.
            if (n % p == 0)
            {
                 
                // If yes, then update
                // n and result
                while (n % p == 0)
                    n /= p;
                result *= (float)(1.0 - (1.0 / (float)p));
            }
        }
 
        // If n has a prime factor
        // greater than sqrt(n)
        // (There can be at-most
        // one such prime factor)
        if (n > 1)
            result *= (float)(1.0 - (1.0 / (float)n));
 
        return (int)result;
    }
 
    // Driver Code
    public static void Main()
    {
        int n;
        for (n = 1; n <= 10; n++)
            Console.WriteLine("phi(" + n + ") = " + phi(n));
    }
}
 
// This code is contributed by nitin mittal.

的PHP

<Φphp
// PHP program to calculate
// Euler's Totient Function
// using Euler's product formula
function phi($n)
{
    // Initialize result as n
    $result = $n;
 
    // Consider all prime factors
    // of n and for every prime
    // factor p, multiply result
    // with (1 - 1/p)
    for ($p = 2; $p * $p <= $n; ++$p)
    {
         
        // Check if p is
        // a prime factor.
        if ($n % $p == 0)
        {
             
            // If yes, then update
            // n and result
            while ($n % $p == 0)
                $n /= $p;
            $result *= (1.0 - (1.0 / $p));
        }
    }
 
    // If n has a prime factor greater
    // than sqrt(n) (There can be at-most
    // one such prime factor)
    if ($n > 1)
        $result *= (1.0 - (1.0 / $n));
 
    return intval($result);
}
 
// Driver Code
for ($n = 1; $n <= 10; $n++)
echo "phi(" .$n. ") =" . phi($n)."\n";
     
// This code is contributed by Sam007
Φ>

Java脚本

// Javascript program to calculate
// Euler's Totient Function
// using Euler's product formula
function phi(n)
{
    // Initialize result as n
    let result = n;
 
    // Consider all prime factors
    // of n and for every prime
    // factor p, multiply result
    // with (1 - 1/p)
    for (let p = 2; p * p <= n; ++p)
    {
         
        // Check if p is
        // a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then update
            // n and result
            while (n % p == 0)
                n /= p;
            result *= (1.0 - (1.0 / p));
        }
    }
 
    // If n has a prime factor greater
    // than sqrt(n) (There can be at-most
    // one such prime factor)
    if (n > 1)
        result *= (1.0 - (1.0 / n));
 
    return parseInt(result);
}
 
// Driver Code
for (let n = 1; n <= 10; n++)
 document.write(`phi(${n}) = ${phi(n)} 
`);       // This code is contributed by _saurabh_jaiswal

输出 :

phi(1) = 1
phi(2) = 1
phi(3) = 2
phi(4) = 2
phi(5) = 4
phi(6) = 2
phi(7) = 6
phi(8) = 4
phi(9) = 6
phi(10) = 4

我们可以避免上述方法中的浮点计算。这个想法是对所有素数及其乘数进行计数,然后从n中减去该计数以获得上位函数值(素数和素数的倍数的gcd不会为1)

1) Initialize result as n
2) Consider every number 'p' (where 'p' varies from 2 to Φn). 
   If p divides n, then do following
   a) Subtract all multiples of p from 1 to n [all multiples of p
      will have gcd more than 1 (at least p) with n]
   b) Update n by repeatedly dividing it by p.
3) If the reduced n is more than 1, then remove all multiples
   of n from result.

下面是上述算法的实现。

C++

// C++ program to calculate Euler's
// Totient Function
#include 
using namespace std;
 
int phi(int n)
{
    // Initialize result as n
    int result = n;
  
    // Consider all prime factors of n
    // and subtract their multiples
    // from result
    for(int p = 2; p * p <= n; ++p)
    {
         
        // Check if p is a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then update n and result
            while (n % p == 0)
                n /= p;
                 
            result -= result / p;
        }
    }
  
    // If n has a prime factor greater than sqrt(n)
    // (There can be at-most one such prime factor)
    if (n > 1)
        result -= result / n;
         
    return result;
}
  
// Driver code
int main()
{
    int n;
    for(n = 1; n <= 10; n++)
    {
        cout << "Phi" << "("
             << n << ")" << " = "
             << phi(n) << endl;
    }
    return 0;
}
 
// This code is contributed by koulick_sadhu

C

// C program to calculate Euler's Totient Function
#include 
 
int phi(int n)
{
    int result = n; // Initialize result as n
 
    // Consider all prime factors of n and subtract their
    // multiples from result
    for (int p = 2; p * p <= n; ++p) {
         
        // Check if p is a prime factor.
        if (n % p == 0) {
             
            // If yes, then update n and result
            while (n % p == 0)
                n /= p;
            result -= result / p;
        }
    }
 
    // If n has a prime factor greater than sqrt(n)
    // (There can be at-most one such prime factor)
    if (n > 1)
        result -= result / n;
    return result;
}
 
// Driver program to test above function
int main()
{
    int n;
    for (n = 1; n <= 10; n++)
        printf("phi(%d) = %d\n", n, phi(n));
    return 0;
}

Java

// Java program to calculate
// Euler's Totient Function
import java.io.*;
 
class GFG
{
static int phi(int n)
{
    // Initialize result as n
    int result = n;
 
    // Consider all prime factors
    // of n and subtract their
    // multiples from result
    for (int p = 2; p * p <= n; ++p)
    {
         
        // Check if p is
        // a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then update
            // n and result
            while (n % p == 0)
                n /= p;
            result -= result / p;
        }
    }
 
    // If n has a prime factor
    // greater than sqrt(n)
    // (There can be at-most
    // one such prime factor)
    if (n > 1)
        result -= result / n;
    return result;
}
 
// Driver Code
public static void main (String[] args)
{
    int n;
    for (n = 1; n <= 10; n++)
        System.out.println("phi(" + n +
                           ") = " + phi(n));
}
}
 
// This code is contributed by ajit

Python3

# Python3 program to calculate
# Euler's Totient Function
def phi(n):
     
    # Initialize result as n
    result = n;
 
    # Consider all prime factors
    # of n and subtract their
    # multiples from result
    p = 2;
    while(p * p <= n):
         
        # Check if p is a
        # prime factor.
        if (n % p == 0):
             
            # If yes, then
            # update n and result
            while (n % p == 0):
                n = int(n / p);
            result -= int(result / p);
        p += 1;
 
    # If n has a prime factor
    # greater than sqrt(n)
    # (There can be at-most
    # one such prime factor)
    if (n > 1):
        result -= int(result / n);
    return result;
 
# Driver Code
for n in range(1, 11):
    print("phi(",n,") =", phi(n));
     
# This code is contributed
# by mits

C#

// C# program to calculate
// Euler's Totient Function
using System;
 
class GFG
{
     
static int phi(int n)
{
// Initialize result as n
int result = n;
 
// Consider all prime 
// factors of n and
// subtract their
// multiples from result
for (int p = 2;
         p * p <= n; ++p)
{
     
    // Check if p is
    // a prime factor.
    if (n % p == 0)
    {
         
        // If yes, then update
        // n and result
        while (n % p == 0)
            n /= p;
        result -= result / p;
    }
}
 
// If n has a prime factor
// greater than sqrt(n)
// (There can be at-most
// one such prime factor)
if (n > 1)
    result -= result / n;
return result;
}
 
// Driver Code
static public void Main ()
{
    int n;
    for (n = 1; n <= 10; n++)
        Console.WriteLine("phi(" + n +
                              ") = " +
                              phi(n));
}
}
 
// This code is contributed
// by akt_mit

的PHP

<Φphp
// PHP program to calculate
// Euler's Totient Function
 
function phi($n)
{
    // Initialize
    // result as n
    $result = $n;
 
    // Consider all prime
    // factors of n and subtract
    // their multiples from result
    for ($p = 2;
         $p * $p <= $n; ++$p)
    {
         
        // Check if p is
        // a prime factor.
        if ($n % $p == 0)
        {
             
            // If yes, then
            // update n and result
            while ($n % $p == 0)
                $n = (int)$n / $p;
            $result -= (int)$result / $p;
        }
    }
 
    // If n has a prime factor
    // greater than sqrt(n)
    // (There can be at-most
    // one such prime factor)
    if ($n > 1)
        $result -= (int)$result / $n;
    return $result;
}
 
// Driver Code
for ($n = 1; $n <= 10; $n++)
    echo "phi(", $n,") =",
          phi($n), "\n";
     
// This code is contributed
// by ajit
Φ>

Java脚本

// Javascript program to calculate
// Euler's Totient Function
 
function phi(n)
{
    // Initialize
    // result as n
    let result = n;
 
    // Consider all prime
    // factors of n and subtract
    // their multiples from result
    for (let p = 2;
         p * p <= n; ++p)
    {
         
        // Check if p is
        // a prime factor.
        if (n % p == 0)
        {
             
            // If yes, then
            // update n and result
            while (n % p == 0)
                n = parseInt(n / p);
            result -= parseInt(result / p);
        }
    }
 
    // If n has a prime factor
    // greater than sqrt(n)
    // (There can be at-most
    // one such prime factor)
    if (n > 1)
        result -= parseInt(result / n);
    return result;
}
 
// Driver Code
for (let n = 1; n <= 10; n++)
    document.write(`phi(${n}) = ${phi(n)} 
`);       // This code is contributed // by _saurabh_jaiswal

输出 :

phi(1) = 1
phi(2) = 1
phi(3) = 2
phi(4) = 2
phi(5) = 4
phi(6) = 2
phi(7) = 6
phi(8) = 4
phi(9) = 6
phi(10) = 4

让我们以一个例子来理解上述算法。

n = 10. 
Initialize: result = 10

2 is a prime factor, so n = n/i = 5, result = 5
3 is not a prime factor.

The for loop stops after 3 as 4*4 is not less than or equal
to 10.

After for loop, result = 5, n = 5
Since n > 1, result = result - result/n = 4

欧拉函数的一些有趣性质
1)对于质数p,Φ(p)为p-1。例如,Φ(5)是4,Φ(7)是6,Φ(13)是12。这很明显,因为p是质数,所以从1到p-1的所有数字的gcd都是1。
2)对于两个数字a和b,如果gcd(a,b)为1,则Φ(ab)=Φ(a)*Φ(b)。例如,Φ(5)是4,Φ(6)是2,所以Φ(30)必须是8,因为5和6是相对质数。
3)对于任意两个质数p和q,Φ(pq)=(p-1)*(q-1)。此属性在RSA算法中使用。
4)如果p是质数,则Φ(p k )= p k – p k-1 。这可以用欧拉的乘积公式证明。
5) n的所有除数的求和函数的值的总和等于n。

高斯

例如,n = 6,n的除数是1、2、3和6。根据高斯,Φ(1)+Φ(2)+Φ(3)+Φ(6)的和应为6。可以通过放置值来验证相同,我们得到(1 +1 + 2 + 2)= 6。
6)欧拉定理表达了最著名和最重要的特征:

The theorem states that if n and a are coprime
(or relatively prime) positive integers, then

aΦ(n) Φ 1 (mod n) 

RSA密码系统基于以下定理:
在m为素数为p的特殊情况下,欧拉定理变成了所谓的费马小定理

ap-1 Φ 1 (mod p)