Given a number N, the task is to check if N is an Maris–McGwire–Sosa Number or not. If N is an Maris–McGwire–Sosa Number then print “Yes” else print “No”.
Maris–McGwire–Sosa Number is a number N such that sum of each digit of N and sum of each digit of prime factorization N gives the same sum as sum of each digit of N + 1 and sum of each digit of prime factorization N + 1. For example, 61 is a MMS Number because Thus 61 –> 6 + 1 (the sum of its digits) + 6 + 1 (since 61 is its prime factorization) = 14 and 62 –> 6 + 2 (the sum of its digits) + 3 + 1 + 2 (since 31 × 2 is its prime factorization) = 14
Examples :
Input : N = 61 Output : Yes Explanation : 61 –> 6 + 1 (the sum of its digits) + 6 + 1 (since 61 is its prime factorization) = 14 and 62 –> 6 + 2 (the sum of its digits) + 3 + 1 + 2 (since 31 × 2 is its prime factorization) = 14 Input : N = 16 Output : No
Approach :
The idea is to find sum of digits and sum of digits of prime factorisation of N and N + 1 and check if the sums of sum of digits and sum of digits of prime factorisation of N and N+1 are equal or not. If the sums of sum of digits and sum of digits of prime factorisation of N and N+1 are equal then the number is a MMS number.
Implementation :
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
// function to find sum of digits of a number
int SOD(int n)
{
int sum = 0;
while (n != 0)
{
int r = n % 10;
sum = sum + r;
n = n/10;
}
return sum;
}
// A function to find sum digits of all prime
// factors of a given number n
int sumPrimeFactors(int n)
{
int sum = 0;
// Print the number of 2s that divide n
while (n % 2 == 0)
{
sum += 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 += SOD(i);
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
{
sum += SOD(n);
}
return sum;
}
// Function to check MMS Pair
bool MMSPair(int n)
{
if ( (SOD(n) + sumPrimeFactors(n)) == (SOD(n+1) + sumPrimeFactors(n+1)) )
return true;
else
return false;
}
// Driver code
int main()
{
int N = 61;
if (MMSPair(N)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}