📜  检查给定编号是否为矿石编号

📅  最后修改于: 2021-05-04 15:47:09             🧑  作者: Mango

给定正整数n,请检查它是否为矿石编号。如果n是矿石编号,则打印“ YES”,否则打印“ NO”。

矿石数:在数学中,矿石数是正整数,其除数具有整数谐波值。矿石编号通常称为谐波除数。矿石编号以Øystein矿石命名。

例如,6有四个除数,即1、2、3和6。
除数的谐波均值是-
6的谐波均值

除数6的谐波平均值是2,是整数。因此,6是矿石数或谐波除数。

前几个矿石编号或谐波除数编号为:

例子

输入:N = 6输出:是输入:N = 4输出:否说明:除数4的谐波均值不是整数。 4的谐波均值

先决条件:

  • 谐波均值
  • 如何生成数字的所有除数

这个想法是生成给定数的所有除数,然后检查除数的谐波均值是否为整数。

  1. 生成给定数字的所有除数–’n’
  2. 计算n除数的谐波均值
  3. 检查谐波均值是否为整数
  4. 如果是,则该编号为矿石编号,否则为

下面是上述方法的实现:

C++
// CPP program to check if the given number is
// Ore number
  
#include 
using namespace std;
  
vector arr;
  
// Function that returns harmonic mean
void generateDivisors(int n)
{
    // Note that this loop runs till square root
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
  
            // If divisors are equal, store 'i'
            if (n / i == i)
                arr.push_back(i);
  
            else // Otherwise store 'i' and 'n/i' both
            {
                arr.push_back(i);
                arr.push_back(n / i);
            }
        }
    }
}
  
// Utility function to calculate harmonic
// mean of the divisors
double harmonicMean(int n)
{
    generateDivisors(n);
  
    // Declare sum variables and initialize
    // with zero.
  
    double sum = 0.0;
  
    int len = arr.size();
  
    // calculate denominator
    for (int i = 0; i < len; i++)
        sum = sum + double(n / arr[i]);
  
    sum = double(sum / n);
  
    // Calculate harmonic mean and return
  
    return double(arr.size() / sum);
}
  
// Function to check if a number is ore number
bool isOreNumber(int n)
{
    // Calculate Harmonic mean of divisors of n
    double mean = harmonicMean(n);
  
    // Check if harmonic mean is an integer or not
    if (mean - int(mean) == 0)
        return true;
    else
        return false;
}
  
// Driver Code
int main()
{
    int n = 28;
  
    if (isOreNumber(n))
        cout << "YES";
    else
        cout << "NO";
  
    return 0;
}


Java
// Java program to check if the given
// number is Ore number
  
import java.util.*;
class GFG {
  
    static Vector arr = new Vector();
  
    // Function that returns harmonic mean.
    static void generateDivisors(int n)
    {
        // Note that this loop runs till square root
        for (int i = 1; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
  
                // If divisors are equal, store 'i'
                if (n / i == i)
                    arr.add(i);
  
                else // Otherwise store 'i' and 'n/i' both
                {
                    arr.add(i);
                    arr.add(n / i);
                }
            }
        }
    }
  
    // Utility function to calculate harmonic mean
    // of the divisors
    static double harmonicMean(int n)
    {
        generateDivisors(n);
  
        // Declare sum variables and initialize
        // with zero.
  
        double sum = 0.0;
  
        int len = arr.size();
  
        // calculate denominator
        for (int i = 0; i < len; i++)
            sum = sum + n / arr.get(i);
  
        sum = sum / n;
  
        // Calculate harmonic mean and return
  
        return arr.size() / sum;
    }
  
    // Function to check if a number
    // is Ore number
    static boolean isOreNumber(int n)
    {
        // Calculate Harmonic mean of divisors of n
        double mean = harmonicMean(n);
  
        // Check if Harmonic mean is an Integer or not
        if (mean - Math.floor(mean) == 0)
            return true;
        else
            return false;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 28;
  
        if (isOreNumber(n))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


Python3
# Python3 program to check if the 
# given number is Ore number 
arr = [] 
  
# Function that returns harmonic mean 
def generateDivisors(n): 
  
    # Note that this loop runs till square root 
    for i in range(1, int(n**(0.5)) + 1): 
        if n % i == 0:
  
            # If divisors are equal, store 'i' 
            if n // i == i: 
                arr.append(i) 
              
            # Otherwise store 'i' and 'n/i' both
            else: 
                arr.append(i) 
                arr.append(n // i) 
          
# Utility function to calculate harmonic 
# mean of the divisors 
def harmonicMean(n): 
  
    generateDivisors(n) 
  
    # Declare sum variables and initialize 
    # with zero. 
    Sum = 0
    length = len(arr) 
  
    # calculate denominator 
    for i in range(0, length): 
        Sum = Sum + (n / arr[i]) 
  
    Sum = Sum / n 
  
    # Calculate harmonic mean and return 
    return length / Sum
  
# Function to check if a number
# is ore number 
def isOreNumber(n): 
  
    # Calculate Harmonic mean of 
    # divisors of n 
    mean = harmonicMean(n) 
  
    # Check if harmonic mean is an 
    # integer or not 
    if mean - int(mean) == 0: 
        return True
    else:
        return False
  
# Driver Code 
if __name__ == "__main__": 
  
    n = 28
  
    if isOreNumber(n) == True: 
        print("YES") 
    else:
        print("NO") 
  
# This code is contributed
# by Rituraj Jain


C#
// C# program to check if the given
// number is Ore number
using System;
using System.Collections;
  
class GFG 
{
  
static ArrayList arr = new ArrayList();
  
// Function that returns harmonic mean.
static void generateDivisors(int n)
{
    // Note that this loop runs
    // till square root
    for (int i = 1; i <= Math.Sqrt(n); i++) 
    {
        if (n % i == 0)
        {
  
            // If divisors are equal, 
            // store 'i'
            if (n / i == i)
                arr.Add(i);
  
            else // Otherwise store 'i' 
                 // and 'n/i' both
            {
                arr.Add(i);
                arr.Add(n / i);
            }
        }
    }
}
  
// Utility function to calculate 
// harmonic mean of the divisors
static double harmonicMean(int n)
{
    generateDivisors(n);
  
    // Declare sum variables and 
    // initialize with zero.
    double sum = 0.0;
  
    int len = arr.Count;
  
    // calculate denominator
    for (int i = 0; i < len; i++)
        sum = sum + n / (int)arr[i];
  
    sum = sum / n;
  
    // Calculate harmonic mean 
    // and return
    return arr.Count / sum;
}
  
// Function to check if a number
// is Ore number
static bool isOreNumber(int n)
{
    // Calculate Harmonic mean of 
    // divisors of n
    double mean = harmonicMean(n);
  
    // Check if Harmonic mean is 
    // an Integer or not
    if (mean - Math.Floor(mean) == 0)
        return true;
    else
        return false;
}
  
// Driver Code
public static void Main()
{
    int n = 28;
  
    if (isOreNumber(n))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
  
// This code is contributed by mits


输出:
YES