Hoax Number In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Hoax Number In C++

Hoax Number In C++

BLUF: Mastering Hoax Number In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Hoax Number In C++

C++ is renowned for its efficiency. Learn how Hoax Number In C++ enables low-level control and high-performance computing in the tutorial below.

An integer is classified as a hoax when the total of the digits of its distinct prime divisors matches the sum of its own digits. It's important to exclude 1 from the calculation of the sum of prime factors' digits because 1 is not a prime number. Ensuring each prime factor is counted only once when it divides the composite number multiple times is crucial.

The main goal of this task is to ascertain if the provided integer, which must be greater than 1, qualifies as a hoax or not. Once all the necessary criteria are met to classify a number as a hoax, we can definitively label it as such.

Example

Input: 58
Output: yes.

The provided figure consists of the numeral 58, which can be factored into its prime components as 2*29. The cumulative value of the digits in the specified number is 5+8=13, while the sum of the digits in its unique prime factors is 2+2+9=13. This particular number is classified as a hoax number due to the equivalence of the sums and the fulfillment of all necessary conditions for a number to qualify as a hoax number.

Example

Input: 54
Output: No

When decomposed into prime factors, the number 54 is represented as 233*3. The prime factors identified are 2 and 3, with a combined digit sum of 2+3=5.

The sum of the digits in the specified number is 5 + 4, resulting in a total of 9. This particular number is genuine as its digit sum differs from the combined sum of its unique prime number digits.

Algorithm:

If the total of a number's digits equals the total of its unique prime factors' digits, the number is considered a hoax. The sum of the digits of the distinct prime factors can be obtained by simply computing all the number's distinct prime factors.

  • We will keep dividing the number by 2 until it becomes odd and store the 2s in another list.
  • Using a for loop, we will iterate from i=3 to i<=sqrt(N), incrementing i by 2 because N must be odd at this point.
  • If N is divisible by i, divide it repeatedly and count how many times you divided. After that, save that value of i in another list.
  • Whenever N is divisible again by the same number (i), keep updating its value of N.
  • Since it is not possible for N to become 1 if it's a prime number greater than 2 using the above steps, check its value with an if statement and if greater than 2 store it into another list.
  • Example:

Let's consider an example to demonstrate the concept of a Hoax number in the C++ programming language.

Example

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

// Function to find distinct prime factors of N and store them in a vector
void findDistinctPrimeFactors(vector<int>& factorsVector, int number) {
    
    if (number % 2 == 0) { // Check if number is divisible by 2
        while (number % 2 == 0) { // Divide number by 2 until remainder is 0
            number = number / 2;
        }
        factorsVector.push_back(2); // Store 2 in the vector
    }
    
    // Iterate for odd numbers from 3 to square root of number
    for (int i = 3; i <= sqrt(number); i += 2) {
        
        if (number % i == 0) { // If number is divisible by i
            while (number % i == 0) { // Divide number by i until remainder is 0
                number = number / i;
            }
            factorsVector.push_back(i); // Store value of i in the vector
        }
    }
    
    // For the case when number is a prime number greater than 2
    if (number > 2) {
        factorsVector.push_back(number);
    }
}

// Function to check if number is a hoax number
bool isHoaxNumber(int number) {
    vector<int> primeFactors; // Vector to store distinct prime factors of number
    
    findDistinctPrimeFactors(primeFactors, number); // Find prime factors of number and store them in vector
    
    if (primeFactors[0] == number) { // For the case when number is a prime number
        return false;
    }
    
    int sumOfPrimeFactors = 0; // Sum of digits of distinct prime factors
    
    // Calculate sum of digits of prime factors
    for (int i = 0; i < primeFactors.size(); i++) {
        int temp = primeFactors[i];
        while (temp > 0) {
            sumOfPrimeFactors += temp % 10; // Add each digit to sumOfPrimeFactors
            temp = temp / 10; // Move to the next digit
        }
    }
    
    int sumOfDigits = 0; // Sum of digits of number
    
    // Calculate sum of digits of number
    int tempNumber = number;
    while (tempNumber > 0) {
        sumOfDigits += tempNumber % 10; // Add each digit to sumOfDigits
        tempNumber /= 10; // Move to the next digit
    }
    
    if (sumOfPrimeFactors == sumOfDigits) { // If sum of digits of prime factors is equal to sum of digits of number
        return true; // Number is a hoax number
    } else {
        return false; // Number is not a hoax number
    }
}

int main() {
    int number;
    cout << "Enter a number to check if it's a hoax number: ";
    cin >> number;
    
    // Call the function to check if number is a hoax number
    if (isHoaxNumber(number)) {
        cout << number << " is a hoax number" << endl;
    } else {
        cout << number << " is not a hoax number" << endl;
    }

    return 0;
}

Output:

Output

Enter a number to check if it's a hoax number: 58
58 is a hoax number
Enter a number to check if it's a hoax number: 345
345 is not a hoax number

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience