Compruebe si el número dado es el número de mineral o no

Dado un entero positivo n, compruebe si es un número de mineral o no. Imprime ‘SÍ’ si n es un número de mineral; de lo contrario, imprime ‘NO’.
Número de mineral : en matemáticas, los números de mineral son números enteros positivos cuyos divisores tienen un valor armónico entero. Los números de mineral a menudo se denominan números de divisores armónicos. Los números de mineral llevan el nombre de Øystein Ore.
Por ejemplo, 6 tiene cuatro divisores, a saber, 1, 2, 3 y 6. 
La media armónica de los divisores es: 
 

Harmonic mean of  6

La media armónica de los divisores de 6 es 2, un número entero. Entonces, 6 es un número de mineral o un número de divisor armónico.
Primero, algunos números de mineral o números de divisores armónicos son: 

1, 6, 28, 140, 270, 496, 672, 1638, 2970, 6200, 8128, 8190 
 

Ejemplos :  

Input : N = 6
Output : Yes

Input : N = 4
Output: No
Explanation : Harmonic mean of divisors of 4 
              is not an Integer. 

Requisito previo: 

La idea es generar todos los divisores del número dado y luego comprobar si la media armónica del divisor es un número entero o no. 

  1. Generar todos los divisores del número dado – ‘n’
  2. Calcular la media armónica de los divisores de n
  3. Compruebe si la media armónica es un número entero o no
  4. En caso afirmativo, entonces el número es un número de mineral de lo contrario no

A continuación se muestra la implementación del enfoque anterior: 

C++

// CPP program to check if the given number is
// Ore number
 
#include <bits/stdc++.h>
using namespace std;
 
vector<int> 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<Integer> arr = new Vector<Integer>();
 
    // 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

Javascript

<script>
 
// Javascript program to check
// if the given number is
// Ore number
 
var arr = [];
 
// Function that returns harmonic mean
function generateDivisors(n)
{
    // Note that this loop runs till square root
    for (var i = 1; i <= Math.sqrt(n); i++) {
        if (n % i == 0) {
 
            // If divisors are equal, store 'i'
            if (n / i == i)
                arr.push(i);
 
            else // Otherwise store 'i' and 'n/i' both
            {
                arr.push(i);
                arr.push(n / i);
            }
        }
    }
}
 
// Utility function to calculate harmonic
// mean of the divisors
function harmonicMean(n)
{
    generateDivisors(n);
 
    // Declare sum variables and initialize
    // with zero.
 
    var sum = 0.0;
 
    var len = arr.length;
 
    // calculate denominator
    for (var i = 0; i < len; i++)
        sum = sum + (n / arr[i]);
 
    sum = (sum / n);
 
    // Calculate harmonic mean and return
 
    return (arr.length / sum);
}
 
// Function to check if a number is ore number
function isOreNumber(n)
{
    // Calculate Harmonic mean of divisors of n
    var mean = harmonicMean(n);
 
    // Check if harmonic mean is an integer or not
    if (mean - parseInt(mean) == 0)
        return true;
    else
        return false;
}
 
// Driver Code
var n = 28;
if (isOreNumber(n))
    document.write( "YES");
else
    document.write( "NO");
 
</script>
Producción

YES

Complejidad de tiempo: O(sqrt(n)), donde n es el número dado.
Espacio auxiliar: O(sqrt(n)), para almacenar el divisor de n en la array

Publicación traducida automáticamente

Artículo escrito por ihritik y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *