Open In App

Droll Numbers

Last Updated : 24 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to check if N is an Droll Number or not. If N is an Droll Number then print “Yes” else print “No”.

A droll Number is a number such that the sum of even prime divisors of N is equaled to the sum of odd prime divisors of N
 

Examples:  

Input: N = 6272 
Output: Yes 
Explanation: 
6272 = 2*2*2*2*2*2*2*7*7 is droll 
since 2+2+2+2+2+2+2 = 14 = 7+7.

Input: N = 10 
Output: No 
  

Approach: The idea is to find the sum of even prime factors and the sum of odd prime factors and check if they are equal or not. If they are equal then N is an Droll Number and print “Yes” for it, else print “No”.

Below is the implementation of the above approach: 

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>


Output: 

Yes

 

Time Complexity: O(sqrt(N))
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads