Highly Totient Number
Last Updated :
22 Jun, 2022
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
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++
#include <bits/stdc++.h>
using namespace std;
int phi( int n)
{
int result = n;
for ( int p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0)
n /= p;
result -= result / p;
}
}
if (n > 1)
result -= result / n;
return result;
}
void Highly_Totient( int n)
{
int count = 0, p_count = -1, i = 1;
map< int , int > mp;
for ( int i = 1; i < 100000; i++)
mp[phi(i)]++;
while (count < n) {
if (mp[i] > p_count)
{
cout << i;
if (count < n-1)
cout << ", " ;
p_count = mp[i];
count++;
}
i++;
}
}
int main()
{
int n = 20;
Highly_Totient(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int phi( int n)
{
int result = n;
for ( int p = 2 ; p * p <= n; ++p)
{
if (n % p == 0 )
{
while (n % p == 0 )
n /= p;
result -= result / p;
}
}
if (n > 1 )
result -= result / n;
return result;
}
static void Highly_Totient( int n)
{
int count = 0 , p_count = - 1 ;
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
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 (mp.containsKey(i)&&mp.get(i) > p_count)
{
System.out.print(i);
if (count < n - 1 )
System.out.print( ", " );
p_count = mp.get(i);
count++;
}
i++;
}
}
public static void main(String[] args)
{
int n = 20 ;
Highly_Totient(n);
}
}
|
Python3
def phi(n):
result = n;
p = 2
while (p * p < = n):
if (n % p = = 0 ):
while (n % p = = 0 ):
n / / = p;
result - = (result / / p);
p + = 1
if (n > 1 ):
result - = (result / / n);
return result;
def Highly_Totient(n):
count = 0
p_count = - 1
mp = dict ()
i = 1
while i < 100000 :
tmp = phi(i)
if tmp not in mp:
mp[tmp] = 0
mp[tmp] + = 1 ;
i + = 1
i = 1
while (count < n):
if ((i in mp) and mp[i] > p_count):
print (i, end = '');
if (count < n - 1 ):
print ( ", " , end = '');
p_count = mp[i];
count + = 1
i + = 1
if __name__ = = '__main__' :
n = 20 ;
Highly_Totient(n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int phi( int n)
{
int result = n;
for ( int p = 2; p * p <= n; ++p)
{
if (n % p == 0)
{
while (n % p == 0)
n /= p;
result -= result / p;
}
}
if (n > 1)
result -= result / n;
return result;
}
static void Highly_Totient( int n)
{
int count = 0, p_count = -1, i;
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 (mp.ContainsKey(i)&&mp[i] > p_count)
{
Console.Write(i);
if (count < n - 1)
Console.Write( ", " );
p_count = mp[i];
count++;
}
i++;
}
}
public static void Main(String[] args)
{
int n = 20;
Highly_Totient(n);
}
}
|
Javascript
<script>
function phi(n) {
var result = n;
for ( var p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0)
n /= p;
result -= result / p;
}
}
if (n > 1)
result -= result / n;
return result;
}
function Highly_Totient(n)
{
var count = 0, p_count = -1;
var mp = new Map();
for (i = 1; i < 100000; i++) {
if (mp.has(phi(i))) {
mp.set(phi(i), mp.get(phi(i)) + 1);
} else {
mp.set(phi(i), 1);
}
}
var i = 1;
while (count < n) {
if (mp.has(i) && mp.get(i) > p_count)
{
document.write(i);
if (count < n - 1)
document.write( ", " );
p_count = mp.get(i);
count++;
}
i++;
}
}
var n = 20;
Highly_Totient(n);
</script>
|
Output:
1, 2, 4, 8, 12, 24, 48, 72, 144, 240, 432, 480, 576, 720, 1152, 1440, 2880, 4320, 5760, 8640
Time Complexity: O(N), as we are using a loop to traverse N times. [ As O(N) > O(sqrt(N)*logN), as we using nested loops for traversing sqrt(N)*logN times ]
Auxiliary Space: O(100000), as we are using extra space for the map.
This method cannot be used to find more than 1000 Highly totient number.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...