📌  相关文章
📜  生成一个数组,其所有元素的 Euler Totient函数之和等于 N

📅  最后修改于: 2021-09-07 04:36:56             🧑  作者: Mango

给定一个正整数N ,任务是生成一个数组,使得每个元素的 Euler Totient函数之和等于N

例子:

方法:给定的问题可以基于欧拉 Totient函数的除数和性质来解决,即,

  • 数量为N <的Euler函数是给出GCD(I,N)1,一个数N可以被表示为N的所有的除数的Euler函数值的总和从1N的整数的数.
  • 因此,这个想法是找到给定数字N的除数作为结果数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to construct the array such
// the sum of values of Euler Totient
// functions of all array elements is N
void constructArray(int N)
{
    // Stores the resutlant array
    vector ans;
 
    // Find divisors in sqrt(N)
    for (int i = 1; i * i <= N; i++) {
 
        // If N is divisible by i
        if (N % i == 0) {
 
            // Push the current divisor
            ans.push_back(i);
 
            // If N is not a
            // perfect square
            if (N != (i * i)) {
 
                // Push the second divisor
                ans.push_back(N / i);
            }
        }
    }
 
    // Print the resultant array
    for (auto it : ans) {
        cout << it << " ";
    }
}
 
// Driver Code
int main()
{
    int N = 12;
 
    // Function Call
    constructArray(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to construct the array such
// the sum of values of Euler Totient
// functions of all array elements is N
static void constructArray(int N)
{
     
    // Stores the resutlant array
    ArrayList ans = new ArrayList();
 
    // Find divisors in sqrt(N)
    for(int i = 1; i * i <= N; i++)
    {
         
        // If N is divisible by i
        if (N % i == 0)
        {
             
            // Push the current divisor
            ans.add(i);
 
            // If N is not a
            // perfect square
            if (N != (i * i))
            {
                 
                // Push the second divisor
                ans.add(N / i);
            }
        }
    }
 
    // Print the resultant array
    for(int it : ans)
    {
        System.out.print(it + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 12;
 
    // Function Call
    constructArray(N);
}
}
 
// This code is contributed by splevel62


Python3
# Python3 program for the above approach
from math import sqrt
 
# Function to construct the array such
# the sum of values of Euler Totient
# functions of all array elements is N
def constructArray(N):
     
    # Stores the resutlant array
    ans = []
 
    # Find divisors in sqrt(N)
    for i in range(1, int(sqrt(N)) + 1, 1):
         
        # If N is divisible by i
        if (N % i == 0):
             
            # Push the current divisor
            ans.append(i)
 
            # If N is not a
            # perfect square
            if (N != (i * i)):
                 
                # Push the second divisor
                ans.append(N / i)
 
    # Print the resultant array
    for it in ans:
        print(int(it), end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    N = 12
     
    # Function Call
    constructArray(N)
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
     
// Function to construct the array such
// the sum of values of Euler Totient
// functions of all array elements is N
static void constructArray(int N)
{
     
    // Stores the resutlant array
    List ans = new List();
 
    // Find divisors in sqrt(N)
    for(int i = 1; i * i <= N; i++)
    {
         
        // If N is divisible by i
        if (N % i == 0)
        {
             
            // Push the current divisor
            ans.Add(i);
 
            // If N is not a
            // perfect square
            if (N != (i * i))
            {
                 
                // Push the second divisor
                ans.Add(N / i);
            }
        }
    }
 
    // Print the resultant array
    foreach(int it in ans)
    {
        Console.Write(it + " ");
    }
}
 
// Driver Code
public static void Main()
{
    int N = 12;
 
    // Function Call
    constructArray(N);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
1 12 2 6 3 4

时间复杂度: O(√N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live