为 N 的每个除数获得的欧拉总函数和
给定一个正整数N ,任务是找到给定数N的所有除数的欧拉总函数之和。
例子:
Input: N = 3
Output: 3
Explanation:
Divisors of 3 are {1, 3}. The Euler totient function for the values 1 and 3 are 1 and 2 respectively.
Therefore, the required sum is 1 + 2 = 3.
Input: N = 6
Output: 6
朴素方法:给定问题可以通过找到N的所有除数来解决,然后打印每个除数的欧拉总函数值的总和作为结果。
时间复杂度: O(N * sqrt(N))
辅助空间: O(1)
有效方法:上述方法也可以通过使用欧拉总函数的性质进行优化,该属性表明所有除数的欧拉总函数的所有值的总和为N 。
因此, N的欧拉函数的所有值之和就是这个数本身。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the sum of Euler
// Totient Function of divisors of N
int sumOfDivisors(int N)
{
// Return the value of N
return N;
}
// Driver Code
int main()
{
int N = 5;
cout << sumOfDivisors(N);
return 0;
}
Java
// Java program for the above approach
public class GFG {
// Function to find the sum of Euler
// Totient Function of divisors of N
static int sumOfDivisors(int N)
{
// Return the value of N
return N;
}
// Driver code
public static void main(String[] args)
{
int N = 5;
System.out.println(sumOfDivisors(N));
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to find the sum of Euler
# Totient Function of divisors of N
def sumOfDivisors(N):
# Return the value of N
return N
# Driver Code
if __name__ == '__main__':
N = 5
print (sumOfDivisors(N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the sum of Euler
// Totient Function of divisors of N
static int sumOfDivisors(int N)
{
// Return the value of N
return N;
}
// Driver code
static void Main()
{
int N = 5;
Console.Write(sumOfDivisors(N));
}
}
// This code is contributed by sanjoy_62.
Javascript
输出:
5
时间复杂度: O(1)
辅助空间: O(1)