An integer is said to be a hoax if the sum of the digits of its unique prime factors is equal to the sum of its own digits as well. Notably, we do not include 1 when considering the sum of prime factors' digits since 1 is not a prime number. Take care to count a prime factor only once when it divides the composite number more than once.
The primary objective in this problem is determining whether the given positive integer (greater than 1) should be considered a hoax or not. Upon establishing that a number satisfies all conditions warranting it to be called a hoax, we can confirm it indeed falls into this category.
Input: 58
Output: yes.
The input contains the number 58, whose prime factorization is 2*29. The total of the digits in the given number is 5+8=13, and the sum of the digits in the number's distinct prime factors is 2+2+9=13. The provided number is a hoax number because the sum is equal and it meets all the requirements needed for a number to be a hoax number.
Input: 54
Output: No
The number 54, when broken down into its prime factors, is 233*3. 2 and 3 are the unique prime factors, whose sum of digits is 2+3=5.
The given number's digit sum is 5 + 4 = 9. The given number is not a hoax because its digit sum does not equal the sum of its distinct 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 us take an example to illustrate the Hoax number in C++.
#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:
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