# To Check if a Number is a Munchhausen Number

Munchhausen Numbers are peculiar numbers which possess a unique property. A number is considered to be munchhausen if the sum of the digits of the number, raised to their own power, is equal to the original number. These numbers are uncommon and not many of them are known. If the definition 00 = 0 is used, then 0 can also be considered a munchhausen number.

The following article provides a method to determine whether a number is munchhausen or not while keeping in mind these characteristics of munchhausen numbers.

## Problem Statement

The task at hand is to check whether a given integer, n, is a Münchhausen number or not i.e. when each digit is raised to its own power and summed, the result is equal to the original number. If it is a Münchhausen number, the program should return true, else it should return false.

### Examples

Input: 1
Output: True


Explanation − (1 raised to the power 1) = 11 = 1.

Since the resultant number is equal to the original number, 1 is a Münchhausen number.

Input: 1603
Output: False


Explanation − (1 raised to the power 1) + (6 raised to the power 6) + (0 raised to the power 0) + (3 raised to the power 3) = 11 + 66 + 00 + 33 ≠ 1603 .

This is equal to 46684. Since the resultant number is not equal to the original number, 1603 is not a Münchhausen number.

Input: 3435
Output: True


Explanation − (3 raised to the power 3) + (4 raised to the power 4) + (3 raised to the power 3) + (5 raised to the power 5) = 33 + 44 + 33 + 55 = 3435.

Since the resultant number is equal to the original number, 3435 is a Münchhausen number.

Input: 4335
Output: False


Explanation − (4 raised to the power 4) + (3 raised to the power 3) + (3 raised to the power 3) +(5 raised to the power 5) = 44 + 33 + 33 + 55 ≠ 4335.

Since the resulting number is not equal to the original number, 4335 is not a Münchhausen number.

## Solution Approach

In order to determine if the provided number is a Münchhausen number, we must know whether the summation of the result of every digit raised to itself is same as the original number. The following method can be used to compute the sum and determine if the outcome matches the original number.

### Algorithm

The approach consists of the following steps −

• Divide the given number into its digits.

• Raise each digit to itself.

• Compare the total sum with the original number.

### Pseudo Code

Function is_munchhausen()

• Initialise sum = 0

• Initialise temp = n

• while (temp > 0)

Initialise digit = temp % 10

sum = sum + pow(digit, digit)

temp = temp / 10

• return sum == n

Function main()

• Initialise n

• if (is_munchhausen())

cout << “Münchhausen Number”

• else

cout << “Non-Münchhausen Number”

• Print output

### Example: C++ Program

The program determines whether a number is a Münchhausen number by calling the is_munchhausen() function. The function uses a temporary variable equal to n and another variable sum to store the summation of the result of each digit raised to itself.

In every iteration of the loop, each individual digit of temp is accessed using the ‘%’ operator. It returns the rightmost digit of the number. The digit is then raised to itself and added to the sum. At the end of each iteration, temp is divided by 10 inorder to access the next digit. The loop runs till temp > 0.

// C++ code for Münchhausen Number
#include <iostream>
#include <cmath>

using namespace std;
// this function is used to check out whether the given number is Münchhausen Number or not
bool is_munchhausen(int n){
int sum = 0;
int temp = n;
while (temp > 0){
int digit = temp % 10; //yields the rightmost digit as remainder
sum = sum + pow(digit, digit);
temp = temp / 10; // yields the remaining number
}
return (sum == n); // returns true if sum is equal to original number
}
// Driver Code
int main(){
int n = 3253;
cout << "input number: " << n << endl;
if (is_munchhausen(n)){
cout << "Münchhausen Number" << endl;
} else {
cout << "Non-Münchhausen Number" << endl;
}
return 0;
}


### Output

input number: 3253
Non-Münchhausen Number


### Time and Space Complexity Analysis

Time Complexity − O(log n) time complexity, where n is the value of the input parameter. This is due to the fact that the number of iterations of the while loop in the function is_munchhausen() depend on the digit count of the given number, which is proportional to log(n) in base 10. The function is called once in the main function hence the overall complexity of the program is proportional to log(n).

Space Complexity − O(1). This function uses a fixed amount of memory to store the integer variables sum and temp regardless of the size of the input parameter, hence its space complexity is constant.

## Conclusion

In conclusion, Munchhausen numbers are unique numbers that are expressed as the sum of their digits raised to themselves. They are not very common and finding them can be a difficult task. The solution approach discussed in this article provides a method to check with ease whether a number is Münchhausen or not in logarithmic time without using auxiliary space. The article explains the concept of Münchhausen numbers in depth using various examples. One can quickly ascertain whether a given number is a Münchhausen number or not using the accompanying C++ code.

Updated on: 19-Apr-2023

135 Views