📌  相关文章
📜  所有小于或等于n的数字的欧拉Totient函数

📅  最后修改于: 2021-05-06 20:52:32             🧑  作者: Mango

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

例如,Φ(4)= 2,Φ(3)= 2和Φ(5)=4。有2个小于或等于4的数相对质数为4,2个小于或等于3的数相对质数素数为3。小于或等于5的4个数相对于素数为5。
在先前的文章中,我们讨论了用于计算Φ(n)的不同方法。

如何为所有小于或等于n的数字计算Φ?

例子:

Input: n = 5
Output: Totient of 1 is 1
        Totient of 2 is 1
        Totient of 3 is 2
        Totient of 4 is 2
        Totient of 5 is 4

强烈建议您最小化浏览器,然后自己尝试。
一个简单的解决方案是为i = 1到n调用Φ(i)。

一个有效的解决方案是使用类似于Eratosthenes筛的想法来预先计算所有值。该方法基于以下产品公式。

欧乐产品

该公式基本上说,对于n的所有素数p,Φ(n)的值等于n乘以(1/1 / p)的乘积。例如Φ(6)= 6 *(1-1 / 2)*(1 – 1/3)= 2。

下面是完整的算法:

1) Create an array phi[1..n] to store Φ values of all numbers 
   from 1 to n.  

2) Initialize all values such that phi[i] stores i.  This
   initialization serves two purposes.
   a) To check if phi[i] is already evaluated or not. Note that
      the maximum possible phi value of a number i is i-1.
   b) To initialize phi[i] as i is a multiple in above product
      formula. 

3) Run a loop for p = 2 to n
    a) If phi[p] is p, means p is not evaluated yet and p is a 
       prime number (similar to Sieve), otherwise phi[p] must 
       have been updated in step 3.b
    b) Traverse through all multiples of p and update all 
       multiples of p by multiplying with (1-1/p).

4) Run a loop from i = 1 to n and print all Ph[i] values.

下面是上述算法的实现。

C++
// C++ program to compute Totient function for
// all numbers smaller than or equal to n.
#include
using namespace std;
 
// Computes and prints totien of all numbers
// smaller than or equal to n.
void computeTotient(int n)
{
    // Create and initialize an array to store
    // phi or totient values
    long long phi[n+1];
    for (int i=1; i<=n; i++)
        phi[i] = i; // indicates not evaluated yet
                    // and initializes for product
                    // formula.
 
    // Compute other Phi values
    for (int p=2; p<=n; p++)
    {
        // If phi[p] is not computed already,
        // then number p is prime
        if (phi[p] == p)
        {
            // Phi of a prime number p is
            // always equal to p-1.
            phi[p] = p-1;
 
            // Update phi values of all
            // multiples of p
            for (int i = 2*p; i<=n; i += p)
            {
            // Add contribution of p to its
            // multiple i by multiplying with
            // (1 - 1/p)
            phi[i] = (phi[i]/p) * (p-1);
            }
        }
    }
 
    // Print precomputed phi values
    for (int i=1; i<=n; i++)
    cout << "Totient of " << i << " is "
        << phi[i] << endl;
}
 
// Driver program to test above function
int main()
{
    int n = 12;
    computeTotient(n);
    return 0;
}


Java
// Java program to compute Totient
// function for all numbers smaller
// than or equal to n.
import java.util.*;
 
class GFG {
     
// Computes and prints totient of all numbers
// smaller than or equal to n.
static void computeTotient(int n) {
     
    // Create and initialize an array to store
    // phi or totient values
    long phi[] = new long[n + 1];
    for (int i = 1; i <= n; i++)
    phi[i] = i; // indicates not evaluated yet
                // and initializes for product
                // formula.
 
    // Compute other Phi values
    for (int p = 2; p <= n; p++) {
         
    // If phi[p] is not computed already,
    // then number p is prime
    if (phi[p] == p) {
         
        // Phi of a prime number p is
        // always equal to p-1.
        phi[p] = p - 1;
 
        // Update phi values of all
        // multiples of p
        for (int i = 2 * p; i <= n; i += p) {
             
        // Add contribution of p to its
        // multiple i by multiplying with
        // (1 - 1/p)
        phi[i] = (phi[i] / p) * (p - 1);
        }
    }
    }
 
    // Print precomputed phi values
    for (int i = 1; i <= n; i++)
    System.out.println("Totient of " + i +
                        " is " + phi[i]);
}
 
// Driver code
public static void main(String[] args) {
     
    int n = 12;
    computeTotient(n);
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to compute
# Totient function for
# all numbers smaller than
# or equal to n.
 
# Computes and prints
# totient of all numbers
# smaller than or equal to n.
def computeTotient(n):
 
    # Create and initialize
    # an array to store
    # phi or totient values
    phi=[]
    for i in range(n + 2):
        phi.append(0)
 
    for i in range(1, n+1):
 
        phi[i] = i # indicates not evaluated yet
                    # and initializes for product
                    # formula.
 
    # Compute other Phi values
    for p in range(2,n+1):
     
        # If phi[p] is not computed already,
        # then number p is prime
        if (phi[p] == p):
         
            # Phi of a prime number p is
            # always equal to p-1.
            phi[p] = p-1
 
            # Update phi values of all
            # multiples of p
            for i in range(2*p,n+1,p):
             
                # Add contribution of p to its
                # multiple i by multiplying with
                # (1 - 1/p)
                phi[i] = (phi[i]//p) * (p-1)
     
 
    # Print precomputed phi values
    for i in range(1,n+1):
        print("Totient of ", i ," is ",
        phi[i])
 
# Driver code
 
n = 12
computeTotient(n)
 
# This code is contributed
# by Anant Agarwal


C#
// C# program to check if given two
// strings are at distance one.
using System;
 
class GFG
{
     
// Computes and prints totient of all
// numbers smaller than or equal to n
static void computeTotient(int n)
{
     
    // Create and initialize an array to
    // store phi or totient values
    long []phi = new long[n + 1];
    for (int i = 1; i <= n; i++)
     
    // indicates not evaluated yet
    // and initializes for product
    // formula.
    phi[i] = i;
     
    // Compute other Phi values
    for (int p = 2; p <= n; p++)
    {
         
    // If phi[p] is not computed already,
    // then number p is prime
    if (phi[p] == p)
    {
         
        // Phi of a prime number p is
        // always equal to p-1.
        phi[p] = p - 1;
 
        // Update phi values of all
        // multiples of p
        for (int i = 2 * p; i <= n; i += p)
        {
             
        // Add contribution of p to its
        // multiple i by multiplying with
        // (1 - 1/p)
        phi[i] = (phi[i] / p) * (p - 1);
         
        }
    }
    }
 
    // Print precomputed phi values
    for (int i = 1; i <= n; i++)
    Console.WriteLine("Totient of " + i +" is " + phi[i]);
}
 
// Driver code
public static void Main()
{
     
    int n = 12;
    computeTotient(n);
}
}
 
// This code is contributed by Sam007.


PHP


Javascript


输出:

Totient of 1 is 1
Totient of 2 is 1
Totient of 3 is 2
Totient of 4 is 2
Totient of 5 is 4
Totient of 6 is 2
Totient of 7 is 6
Totient of 8 is 4
Totient of 9 is 6
Totient of 10 is 4
Totient of 11 is 10
Totient of 12 is 4

当我们有大量查询来计算目标函数时,可以使用相同的解决方案。