Hoax Number


The problem statement includes checking if the given number N, which will be the user input, is a hoax number or not.

A Hoax number is a composite number whose sum of digits of its distinct prime factors is equal to the sum of the digits of the composite number itself. Since 1 is not a prime number, we don’t consider 1 as a sum of digits of distinct prime numbers. If a prime number is a factor of the composite number more than once, it is just considered once while taking the sum of digits of prime factors.

In this problem, we will be given a positive integer greater than 1 and our task is to check if the provided number is a hoax number or not. We will check the conditions for a number to be a hoax number, if the number satisfies all the conditions therefore it is a hoax number.

Let’s understand the problem with the below examples.

Input

N= 18

Output

No

Explanation − The prime factorisation of the given number i.e. 18 is 2*3*3. The distinct prime factors are 2 and 3 whose sum of digits will be 2+3=5.

The sum of the digits of the given number is 1+8=9. Since the sum of the digits of the number is not equal to the sum of the digits of the distinct prime numbers, the given number is not a hoax number.

Input

N=58

Output

Yes

Explanation − The given number in the input is 58 whose prime factorisation is 2*29. The sum of the digits of the distinct prime factors of the number is 2+2+9=13 and the sum of the digits of the given number is 5+8=13. Since the sum is equal, the given number is a hoax number as it satisfies all the conditions for a number to be a hoax number.

Input

N=19

Output

No

Explanation − The number given in the input is 19. A hoax number is always a composite number but here 19 is a prime number, hence 19 is not a hoax number.

Let’s understand the algorithm to check the number if it is a hoax number or not by checking the conditions of a number to be a hoax number.

Algorithm

For a number to be a hoax number, the sum of the digits of the number must be equal to the sum of the digits of its distinct prime factors. We will simply calculate all the distinct prime factors of the number to get the sum of digits of the distinct prime factors.

Calculating distinct prime factors of a number:

We will make a function which returns the vector containing distinct prime factors of the number. To store all the distinct prime factors of the number, we will follow the below steps:

  • If the number is divisible by 2 we will divide the number by 2 until it is an odd number and store 2 in the vector.

  • By now N must be odd, so we will iterate in a for loop from i=3 to i<=sqrt(N) for odd numbers only.

  • Now if N is divisible by i, keep dividing N by i until the remainder is equal to zero and then store the value of i in the vector.

  • Keep updating the value of N everytime it is divisible by i.

  • If N is a prime number and greater than 2, it cannot become 1 with the above steps so we will check the value of N using an if statement and if it is greater than 2, store the value in the vector.

Following these steps, we will get the distinct prime factors of the given number. The number should be a composite number only so we will check if the value stored in the vector is not equal to the number for the case when N is a prime number.

Otherwise we will iterate in the vector and calculate the sum of the digits of the distinct prime factors of the number using the modulo operator. Then, we will calculate the sum of digits of the number N. If the sum of both is equal, we will return true as it is a hoax number, else we will return false.

We will use this algorithm in our approach in order to check if the number is a hoax number or not.

Approach

The steps to follow to implement the algorithm to check if the number is a hoax number or not are given below:

  • We will create a boolean function to check if the number is a hoax number or not.

  • Initialise a vector to store the distinct prime factors of the number N.

  • We will update the vector by calling another function we will create to get the distinct prime factors of N following the steps mentioned in the algorithm.

  • Now we will check if the value at the first index in the vector is equal to N or not. If it is equal to N, return false as N is a prime number and a hoax number is always a composite number.

  • If N is a composite number, then we will calculate the sum of digits of distinct prime factors by iterating in the vector. We will calculate the digits of the prime factor using the modulo operator and then divide the number by 10 to get the next digit. We will store the sum of digits of each prime factor in a variable to get the sum of digits of all distinct prime factors.

  • Now calculate the sum of digits of the number N using the same logic and store it in a variable.

  • If the sum of digits of distinct prime factors of N and sum of digits of N is equal, return true as the number N is a hoax number otherwise, return false.

Example

//C++ code to check if N is a hoax number or not

#include <bits/stdc++.h>

using namespace std;

//function to store the distinct prime factors of N in vector
void factors(vector<int>& v, int N){
    
    if(N%2==0){ //check if N is divisible by 2
        
        while(N%2==0){ //divide N by 2 until remainder is equal to 0 and update N everytime
            N = N/2;
        }
        v.push_back(2); //store 2 in the vector
    }
    
    //N must be odd now so iterate for odd numbers from i=3 to sqrt(N)
    for(int i=3;i<=sqrt(N);i += 2){
        
        if(N%i==0){ //if N is divisible by i
            while(N%i==0){ //divide N by i until remainder is 0 and update N 
                N = N/i;
            }
            v.push_back(i); //store value of i in the vector
        }
    }
    
    //for the case when N is a prime number greater than 2
    if(N>2){
        v.push_back(N);
    }
}

//function to check sum of digits of distinct prime factors of N and N
bool check(int N){
    vector<int> p; //vector to store distinct prime factors of N
    
    factors(p,N); //calling the function to store prime factors of N in vector
    
    if(p[0]==N){ //for the case when N is a prime number
        return false;
    }
    
    int sum_factors=0; //to store sum of digits of distinct prime factors
    
    //to calculate sum of digits of prime factors
    for(int i=0;i<p.size();i++){
        while(p[i]>0){ //adding each digit in the variable using modulo operator
            sum_factors += p[i]%10;
            p[i]=p[i]/10;
        }
    }
    
    int sum_number=0;  //to store sum of digits of N
    
    //to calculate sum of digits of N
    while(N>0){
        sum_number += N%10; //adding each digit using modulo 10
        N /=10; //dividing N by 10 to get the next digit
    }
    
    if(sum_factors==sum_number){ //N is a hoax number if sum is equal
        return true;
    }
    else{
        return false;
    }
    
    
}

int main()
{
    int N;
    N=234;
    
    //calling the function
    if(check(N)==true){
        cout<<N<<" is a hoax number"<<endl;
    }
    else{
        cout<<N<<" is not a hoax number"<<endl;
    }

    return 0;
}

Output

234 is a hoax number

Time complexity− $\mathrm{O(\sqrt{N}*\log N)}$

Space complexity − O(1)

Conclusion

The concept of hoax number and the specific conditions for a number to be a hoax number was discussed in the article. We came up with an efficient algorithm in C++ in order to check if the given number is a hoax number or not by calculating prime factors of the number and then checking the sum of digits.

I hope you have cleared all your doubts about the topic and understand the approach after reading this article.

Updated on: 28-Aug-2023

121 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements