Números divertidos

Dado un número N , la tarea es comprobar si N es un Número Droll o no. Si N es un número Droll , escriba «Sí» , de lo contrario, escriba «No» .

Un Número divertido es un número tal que la suma de los divisores primos pares de N es igual a la suma de los divisores primos impares de N
 

Ejemplos:  

Entrada: N = 6272 
Salida: Sí 
Explicación: 
6272 = 2*2*2*2*2*2*2*7*7 es gracioso 
ya que 2+2+2+2+2+2+2 = 14 = 7+ 7.

Entrada: N = 10 
Salida: No 
  

Planteamiento: La idea es encontrar la suma de los factores primos pares y la suma de los factores primos impares y verificar si son iguales o no. Si son iguales, entonces N es un Número Droll e imprime «Sí» , de lo contrario, imprime «No» .

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

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check droll numbers
bool isDroll(int n)
{
    if (n == 1)
        return false;
 
    // To store sum of even prime factors
    int sum_even = 0;
 
    // To store sum of odd prime factors
    int sum_odd = 0;
 
    // Add the number of 2s
    // that divide n in sum_even
    while (n % 2 == 0) {
        sum_even += 2;
        n = n / 2;
    }
 
    // N must be odd at this point.
    // So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2) {
 
        // While i divides n,
        // print i and divide n
        while (n % i == 0) {
            sum_odd += i;
            n = n / i;
        }
    }
 
    // This condition is to handle
    // the case when n is a prime
    // number greater than 2
    if (n > 2)
        sum_odd += n;
 
    // Condition to check droll number
    return sum_even == sum_odd;
}
 
Driver Code int main()
{
    // Given Number N
    int N = 72;
 
    // Function Call
    if (isDroll(N))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Java

// Java program for the above approach
class GFG{
 
// Function to check droll numbers
static boolean isDroll(int n)
{
    if (n == 1)
        return false;
 
    // To store sum of even prime factors
    int sum_even = 0;
 
    // To store sum of odd prime factors
    int sum_odd = 0;
 
    // Add the number of 2s
    // that divide n in sum_even
    while (n % 2 == 0)
    {
        sum_even += 2;
        n = n / 2;
    }
 
    // N must be odd at this point.
    // So we can skip
    // one element (Note i = i +2)
    for(int i = 3; i <= Math.sqrt(n);
                   i = i + 2)
    {
        
       // While i divides n,
       // print i and divide n
       while (n % i == 0)
       {
           sum_odd += i;
           n = n / i;
       }
    }
     
    // This condition is to handle
    // the case when n is a prime
    // number greater than 2
    if (n > 2)
        sum_odd += n;
 
    // Condition to check droll number
    return sum_even == sum_odd;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Number N
    int n = 72;
 
    // Function Call
    if (isDroll(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by Pratima Pandey

Python3

# Python3 program for the above approach
import math;
 
# Function to check droll numbers
def isDroll(n):
 
    if (n == 1):
        return False;
 
    # To store sum of even prime factors
    sum_even = 0;
 
    # To store sum of odd prime factors
    sum_odd = 0;
 
    # Add the number of 2s
    # that divide n in sum_even
    while (n % 2 == 0):
        sum_even += 2;
        n = n // 2;
     
    # N must be odd at this point.
    # So we can skip
    # one element (Note i = i +2)
    for i in range(3, int(math.sqrt(n)) + 1, 2):
 
        # While i divides n,
        # print i and divide n
        while (n % i == 0):
            sum_odd += i;
            n = n // i;
         
    # This condition is to handle
    # the case when n is a prime
    # number greater than 2
    if (n > 2):
        sum_odd += n;
 
    # Condition to check droll number
    return sum_even == sum_odd;
 
# Driver Code
 
# Given Number N
N = 72;
 
# Function Call
if (isDroll(N)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Code_Mech

C#

// C# program for the above approach
using System;
class GFG{
 
// Function to check droll numbers
static bool isDroll(int n)
{
    if (n == 1)
        return false;
 
    // To store sum of even prime factors
    int sum_even = 0;
 
    // To store sum of odd prime factors
    int sum_odd = 0;
 
    // Add the number of 2s
    // that divide n in sum_even
    while (n % 2 == 0)
    {
        sum_even += 2;
        n = n / 2;
    }
 
    // N must be odd at this point.
    // So we can skip
    // one element (Note i = i +2)
    for(int i = 3; i <= Math.Sqrt(n);
                   i = i + 2)
    {
        
       // While i divides n,
       // print i and divide n
       while (n % i == 0)
       {
           sum_odd += i;
           n = n / i;
       }
    }
     
    // This condition is to handle
    // the case when n is a prime
    // number greater than 2
    if (n > 2)
        sum_odd += n;
 
    // Condition to check droll number
    return sum_even == sum_odd;
}
 
// Driver code
public static void Main()
{
     
    // Given Number N
    int n = 72;
 
    // Function Call
    if (isDroll(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Code_Mech

Javascript

<script>
 
// JavaScript program for the above approach
 
// Function to check droll numbers
function isDroll(n)
{
    if (n == 1)
        return false;
 
    // To store sum of even prime factors
    let sum_even = 0;
 
    // To store sum of odd prime factors
    let sum_odd = 0;
 
    // Add the number of 2s
    // that divide n in sum_even
    while (n % 2 == 0)
    {
        sum_even += 2;
        n = n / 2;
    }
 
    // N must be odd at this point.
    // So we can skip
    // one element (Note i = i +2)
    for(let i = 3;
            i <= Math.sqrt(n);
            i = i + 2)
    {
        
       // While i divides n,
       // print i and divide n
       while (n % i == 0)
       {
           sum_odd += i;
           n = n / i;
       }
    }
     
    // This condition is to handle
    // the case when n is a prime
    // number greater than 2
    if (n > 2)
        sum_odd += n;
 
    // Condition to check droll number
    return sum_even == sum_odd;
}
 
// Driver Code
 
// Given Number N
let n = 72;
 
// Function Call
if (isDroll(n))
    document.write("Yes");
else
    document.write("No");
     
// This code is contributed by susmitakundugoaldanga
 
</script>
Producción: 

Yes

 

Complejidad del tiempo: O(sqrt(N))
 

Publicación traducida automáticamente

Artículo escrito por spp____ 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 *