Home > Article > Backend Development > Check if a number is a Munchhausen number
The Munchhausen number is an odd number with unique properties. A number is considered a Munchhausen number if the sum of its digits (its powers) is equal to the original number. These numbers are uncommon and many of them are unknown. If the definition of 00 = 0 is used, then 0 can also be considered the Munchhausen number.
The following article provides a method to determine whether a number is a Munchhausen number, keeping in mind these characteristics of Munchhausen numbers.
The current task is to check whether a given integer n is a Münchhausen number, i.e. when each number 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, otherwise it should return false.
Input: 1 Output: True
Explanation - (1 to the 1st power) = 11 = 1.
Since the resulting number is equal to the original number, 1 is the Munchhausen number.
Input: 1603 Output: False
Explanation − (1 to the 1st power) (6 to the 6th power) (0 to the 0th power) (3 to the 3rd power) = 11 66 00 33 ≠ 1603.
This is equal to 46684. Since the resulting number is not equal to the original number, 1603 is not a Munchhausen number.
Input: 3435 Output: True
Explanation − (3 to the 3rd power) (4 to the 4th power) (3 to the 3rd power) (5 to the 5th power) = 33 44 33 55 = 3435.
Since the resulting number is equal to the original number, 3435 is the Munchhausen number.
Input: 4335 Output: False
Explanation − (4 to the 4th power) (3 to the 3rd power) (3 to the 3rd power) (5 to the 5th power) = 44 33 33 55 ≠ 4335.
Since the resulting number is not equal to the original number, 4335 is not a Munich number.
In order to tell whether the number provided is a Münchhausen number, we must know whether the result of adding each number to itself is the same as the original number. You can use the following methods to calculate the sum and determine whether the result matches the original number.
The method includes the following steps -
Decompose the given number into its individual digits.
Raise each number to itself.
Add results.
Compare the sum to the original number.
Show answer.
Function is_munchhausen()
Initialize sum = 0
Initialization temp = n
At the same time (temperature > 0)
Initialization digit = temp % 10
sum = sum pow(number, number)
Temperature=Temperature/10
Return sum==n
Function main()
Initialize n
if (is_munchhausen())
cout
other
cout
Print output
The program determines whether a number is a Münchhausen number by calling the is_munchhausen() function. This function uses a temporary variable equal to n and another variable sum to store the sum of the results of each number summed over itself.
In each iteration of the loop, use the '%' operator to access each single digit of temp. It returns the rightmost digit of the number. That number is then raised to itself and added to the total. At the end of each iteration, temp is divided by 10 to access the next number. The loop runs until 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; }
input number: 3253 Non-Münchhausen Number
Time complexity - O(log n) time complexity, where n is the value of the input parameter. This is because the number of iterations of the while loop in the function is_munchhausen() depends on the number of digits in the given number, which is proportional to log(n) base 10. This function is called only once in the main function, so the overall complexity of the program is proportional to log(n).
Space complexity - O(1). This function uses fixed memory to store the integer variables sum and temp, so its space complexity is constant regardless of the size of the input arguments.
In summary, a Münchhausen number is a unique number expressed as the sum of its own numbers. They are not common and finding them can be a difficult task. The solution discussed in this article provides a way to easily check whether a number is Münchhausen in logarithmic time without using auxiliary space. This article explains the concept of Munchhausen number in depth using various examples. Use the included C code to quickly determine whether a given number is a Münchhausen number.
The above is the detailed content of Check if a number is a Munchhausen number. For more information, please follow other related articles on the PHP Chinese website!