📜  最高人数

📅  最后修改于: 2021-04-24 15:18:53             🧑  作者: Mango

高托宾数k是对方程φ(x)= k有更多解的整数,其中φ是欧拉的托宾函数
序列 :

解释 :

  • 1个有2个解决方案
  • 2有3个解决方案
  • 4有4个解决方案
  • 8有5个解决方案
  • 12有6个解决方案
  • 24有10个解决方案

对于给定的N ,任务是打印第一个N个高强度数字。
例子:

方法:
一种有效的方法是将最多10 5的所有φ(x)值及其频率存储在映射中,然后从1开始循环运行,直到高度托宾数的计数小于N为止。对于每一个,我们会检查的频率比以前更大的元素,如果是,那么打印数量,并增加了其他数增量的数量。
下面是上述方法的实现:

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 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


Python3
# Python3 program to find highly totient numbers
 
# Function to find euler totient number
def phi(n):
 
    result = n; # Initialize result as n
 
    # Consider all prime factors of n and
    # subtract their multiples from result
    p = 2
 
    while(p*p <= n):
 
        # 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);
        p += 1
         
 
    # 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
def Highly_Totient(n):
 
    # Count of Highly totient numbers
    # and value of count of phi of previous numbers
    count = 0
    p_count = -1
 
    # Store all the values of phi(x) upto
    # 10^5 with frequencies
    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 count is greater than count of
        # previous element
        if ((i in mp) and  mp[i] > p_count):
 
            # Display the number
            print(i, end = '');
             
            if(count < n - 1):
                print(", ", end = '');
 
            # Store the value of phi
            p_count = mp[i];
            count += 1
        i += 1
     
# Driver code
if __name__=='__main__':
 
    n = 20;
     
    # Function call
    Highly_Totient(n);
     
    # This code is contributed by rutvik_56


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 mp = new Dictionary();
    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


输出:

此方法不能用于查找超过1000个“高姿态”号码。