Sum of Euler Totient Functions obtained for each divisor of N
Last Updated :
28 May, 2021
Given a positive integer N, the task is to find the sum of the Euler Totient Function for all the divisor of the given number N.
Examples:
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
Naive Approach: The given problem can be solved by finding all the divisors of N and then print the sum of values of the Euler totient function for every divisor as the result.
Time Complexity: O(N * sqrt(N))
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using the property of the Euler totient function which states that the sum of all the values of the euler totient function of all the divisors is N.
Therefore, the sum of all values of the Euler totient function of N is the number itself.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int sumOfDivisors( int N)
{
return N;
}
int main()
{
int N = 5;
cout << sumOfDivisors(N);
return 0;
}
|
Java
public class GFG {
static int sumOfDivisors( int N)
{
return N;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(sumOfDivisors(N));
}
}
|
Python3
def sumOfDivisors(N):
return N
if __name__ = = '__main__' :
N = 5
print (sumOfDivisors(N))
|
C#
using System;
class GFG{
static int sumOfDivisors( int N)
{
return N;
}
static void Main()
{
int N = 5;
Console.Write(sumOfDivisors(N));
}
}
|
Javascript
<script>
function sumOfDivisors(N){
return N;
}
let N = 5;
document.write(sumOfDivisors(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...