Euler Totient Function Sum

Hard
0/120
Average time to solve is 60m
profile
Contributed by
6 upvotes
Asked in companies
DirectiCodenationCogoport

Problem statement

You are given a positive integer β€˜N’, your task is to return an array/list of integers such that the sum of Euler Totient Function values of each integer of the array is equal to β€˜N’.

Euler Totient Function:

In number theory, Euler's totient function counts the positive integers up to a given integer β€˜N’ that is relatively prime to β€˜N’.

More details about Euler’s Totient Function can be found here

For Example:
For β€˜N’ = 10, if we take the array as 1, 2, 5, 10,
The Euler totient function value of 1 is 1.
The Euler totient function value of 2 is 1.
The Euler totient function value of 5 is 4.
The Euler totient function value of 10 is 4.
So, the summation will be 1 + 1 + 4 + 4 = 10.
Hence this is a valid array for β€˜N’ = 10.

Note:

The solution will be verified by the actual value of the Euler Totient Function of 'N' and the sum of the Euler Totient Function of all the values returned. In the output, only the 'YES' or 'NO' is printed, according to the correctness of the solution.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer β€˜T’, representing the number of test cases.

The first line of each test case contains a single integer β€˜N’, representing the sum of the Euler Totient Function values of all the elements of the output array.
Output Format:
For each test case, Return all the elements of the array satisfying the condition given in the problem statement.

 If there are multiple answers possible print any one of them.

Output for each test case will be printed in a separate line.
Note
You are not required to print anything; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 100
1 <= N <= 10^3

Time Limit: 1 sec.
Hint

Try to use some property of the Euler Totient Function.

Approaches (2)
Brute Force

We will use the divisor sum property of the Euler Totient Function to solve the problem. This property states that any positive integer β€˜N’ can be represented as the summation of the Euler Totient Function values of all the divisors of β€˜N’.

So, now our task is to only find all the divisors of β€˜N’. We can do it by iterating over 1 to β€˜N’ and storing all the numbers which divide β€˜N’.

 

Algorithm:
 

  1. Initialize a vector β€˜ANS’ to store the divisors.
  2. Start iterating from 1 to β€˜N’(say iterator i )
    1. If i is a divisor of β€˜N’ ( N % i == 0 ), store it in the β€˜ANS’ vector.
  3. Return the β€˜ANS’ vector.
Time Complexity

O(N), where β€˜N’ is the given number. 

 

We have to check every number from 1 till β€˜N’ to find factors. So the time complexity will be O(N).

Space Complexity

O(1).

 

Constant extra space is used.

Code Solution
(100% EXP penalty)
Euler Totient Function Sum
Full screen
Console