# Highly Totient Number

A highly totient number k is an integer that has more solutions to the equation φ(x) = k, where φ is Euler’s totient function

The sequence :

1, 2, 4, 8, 12, 24, 48, 72, 144, 240, 432, 480, 576, 720, 1152, 1440, 2880, 4320, 5760, 8640

Explanation :

• 1 has 2 solutions
• 2 has 3 solutions
• 4 has 4 solutions
• 8 has 5 solutions
• 12 has 6 solutions
• 24 has 10 solutions

For a given N, the task is to print first N highly totient numbers.

Examples:

Input : N = 10
Output : 1, 2, 4, 8, 12, 24, 48, 72, 144, 240

Input : N = 20
Output : 1, 2, 4, 8, 12, 24, 48, 72, 144, 240, 432, 480, 576, 720, 1152, 1440, 2880, 4320, 5760, 8640

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
An efficient approach is to store all the values of φ(x) up to 105 in a map along with their frequencies and then run a loop from 1 until the count of Highly totient number is less than N. For each i we will check if the frequency of i is greater than the previous element, if yes then print the number and increase the count else increment the number .

Below is the implementation of the above approach :

## C++

 `// CPP program to find highly totient numbers ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find euler totient number ` `int` `phi(``int` `n) ` `{ ` `    ``int` `result = n; ``// Initialize result as n ` ` `  `    ``// Consider all prime factors of n and  ` `    ``// subtract their multiples from result ` `    ``for` `(``int` `p = 2; p * p <= n; ++p) { ` ` `  `        ``// Check if p is a prime factor. ` `        ``if` `(n % p == 0) { ` ` `  `            ``// If yes, then update n and result ` `            ``while` `(n % p == 0) ` `                ``n /= p; ` `            ``result -= result / p; ` `        ``} ` `    ``} ` ` `  `    ``// If n has a prime factor greater than sqrt(n) ` `    ``// (There can be at-most one such prime factor) ` `    ``if` `(n > 1) ` `        ``result -= result / n; ` `    ``return` `result; ` `} ` ` `  `// Function to find first n highly totient numbers ` `void` `Highly_Totient(``int` `n) ` `{ ` `    ``// Count of Highly totient numbers ` `    ``// and value of count of phi of previous numbers ` `    ``int` `count = 0, p_count = -1, i = 1; ` ` `  `    ``// Store all the values of phi(x) upto ` `    ``// 10^5 with frequencies ` `    ``map<``int``, ``int``> mp; ` ` `  `    ``for` `(``int` `i = 1; i < 100000; i++) ` `        ``mp[phi(i)]++; ` ` `  `    ``while` `(count < n) { ` `         `  `        ``// If count is greater than count of ` `        ``// previous element ` `        ``if` `(mp[i] > p_count) ` `        ``{ ` `            ``// Display the number ` `            ``cout << i; ` `             `  `            ``if``(count < n-1) ` `                ``cout << ``", "``; ` ` `  `            ``// Store the value of phi ` `            ``p_count = mp[i]; ` `            ``count++; ` `        ``} ` `        ``i++; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 20; ` `     `  `    ``// Function call ` `    ``Highly_Totient(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find highly totient numbers ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to find euler totient number ` `static` `int` `phi(``int` `n) ` `{ ` `    ``int` `result = n; ``// Initialize result as n ` ` `  `    ``// Consider all prime factors of n and  ` `    ``// subtract their multiples from result ` `    ``for` `(``int` `p = ``2``; p * p <= n; ++p) ` `    ``{ ` ` `  `        ``// Check if p is a prime factor. ` `        ``if` `(n % p == ``0``)  ` `        ``{ ` ` `  `            ``// If yes, then update n and result ` `            ``while` `(n % p == ``0``) ` `                ``n /= p; ` `            ``result -= result / p; ` `        ``} ` `    ``} ` ` `  `    ``// If n has a prime factor greater than sqrt(n) ` `    ``// (There can be at-most one such prime factor) ` `    ``if` `(n > ``1``) ` `        ``result -= result / n; ` `    ``return` `result; ` `} ` ` `  `// Function to find first n highly totient numbers ` `static` `void` `Highly_Totient(``int` `n) ` `{ ` `    ``// Count of Highly totient numbers ` `    ``// and value of count of phi of previous numbers ` `    ``int` `count = ``0``, p_count = -``1``; ` ` `  `    ``// Store all the values of phi(x) upto ` `    ``// 10^5 with frequencies ` `    ``HashMap mp = ``new` `HashMap(); ` ` `  `    ``for` `(``int` `i = ``1``; i < ``100000``; i++) ` `    ``{ ` `        ``if``(mp.containsKey(phi(i))) ` `        ``{ ` `            ``mp.put(phi(i), mp.get(phi(i)) + ``1``); ` `        ``} ` `        ``else` `        ``{ ` `            ``mp.put(phi(i), ``1``); ` `        ``} ` `    ``} ` `     `  `    ``int` `i = ``1``; ` `    ``while` `(count < n) ` `    ``{ ` `         `  `        ``// If count is greater than count of ` `        ``// previous element ` `        ``if` `(mp.containsKey(i)&&mp.get(i) > p_count) ` `        ``{ ` `            ``// Display the number ` `            ``System.out.print(i); ` `             `  `            ``if``(count < n - ``1``) ` `                ``System.out.print(``", "``); ` ` `  `            ``// Store the value of phi ` `            ``p_count = mp.get(i); ` `            ``count++; ` `        ``} ` `        ``i++; ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``20``; ` `     `  `    ``// Function call ` `    ``Highly_Totient(n); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## C#

 `// C# program to find highly totient numbers ` `using` `System; ` `using` `System.Collections.Generic; ` `     `  `class` `GFG  ` `{ ` ` `  `// Function to find euler totient number ` `static` `int` `phi(``int` `n) ` `{ ` `    ``int` `result = n; ``// Initialize result as n ` ` `  `    ``// Consider all prime factors of n and  ` `    ``// subtract their multiples from result ` `    ``for` `(``int` `p = 2; p * p <= n; ++p) ` `    ``{ ` ` `  `        ``// Check if p is a prime factor. ` `        ``if` `(n % p == 0)  ` `        ``{ ` ` `  `            ``// If yes, then update n and result ` `            ``while` `(n % p == 0) ` `                ``n /= p; ` `            ``result -= result / p; ` `        ``} ` `    ``} ` ` `  `    ``// If n has a prime factor greater than sqrt(n) ` `    ``// (There can be at-most one such prime factor) ` `    ``if` `(n > 1) ` `        ``result -= result / n; ` `    ``return` `result; ` `} ` ` `  `// Function to find first n highly totient numbers ` `static` `void` `Highly_Totient(``int` `n) ` `{ ` `    ``// Count of Highly totient numbers ` `    ``// and value of count of phi of  ` `    ``// previous numbers ` `    ``int` `count = 0, p_count = -1, i; ` ` `  `    ``// Store all the values of phi(x) upto ` `    ``// 10^5 with frequencies ` `    ``Dictionary<``int``,  ` `               ``int``> mp = ``new` `Dictionary<``int``,  ` `                                        ``int``>(); ` `    ``for` `(i = 1; i < 100000; i++) ` `    ``{ ` `        ``if``(mp.ContainsKey(phi(i))) ` `        ``{ ` `            ``mp[phi(i)] = mp[phi(i)] + 1; ` `        ``} ` `        ``else` `        ``{ ` `            ``mp.Add(phi(i), 1); ` `        ``} ` `    ``} ` `     `  `    ``i = 1; ` `    ``while` `(count < n) ` `    ``{ ` `         `  `        ``// If count is greater than count of ` `        ``// previous element ` `        ``if` `(mp.ContainsKey(i)&&mp[i] > p_count) ` `        ``{ ` `            ``// Display the number ` `            ``Console.Write(i); ` `             `  `            ``if``(count < n - 1) ` `                ``Console.Write(``", "``); ` ` `  `            ``// Store the value of phi ` `            ``p_count = mp[i]; ` `            ``count++; ` `        ``} ` `        ``i++; ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `n = 20; ` `     `  `    ``// Function call ` `    ``Highly_Totient(n); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

1, 2, 4, 8, 12, 24, 48, 72, 144, 240, 432, 480, 576, 720, 1152, 1440, 2880, 4320, 5760, 8640

This method cannot be used to find more than 1000 Highly totient number.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Third year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akanksha_Rai, Rajput-Ji

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.