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

Munchhausen Number In C++

BLUF: Mastering Munchhausen 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: Munchhausen Number In C++

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

In this tutorial, we will explore the process of verifying if a given number qualifies as a Munchhausen number, accompanied by illustrative examples.

What are Munchhausen Numbers?

Munchhausen Numbers are unique numbers with a specific characteristic. A number qualifies as a Munchhausen number when the sum of its individual digits raised to the power of themselves equals the original number. These numbers are uncommon and therefore not widely recognized. Additionally, by considering the definition 00 = 0, we can include 0 in the category of Munchhausen numbers.

Problem Statement

The task is to verify if a given number n is a Munchausen number, where the sum of each digit raised to its own power equals n. When n is a Munchausen number, the function should output true; otherwise, it should return false.

Example:

Input: 1

  • exponentiated by 1 equals 1, resulting in a true output.
  • Approaches 1: Brute Force Approach

This method enables the utilization of every single digit within the specified range. The calculation involves raising each digit to a specific power, summing them up, and comparing the result to the initial number. The computation of this sum can be quite time-intensive, particularly when dealing with larger ranges. It may be more feasible to apply this approach to smaller ranges for efficiency.

Example:

Let's consider an illustration to explain the concept of Munchhausen numbers in C++ through the implementation of a Brute Force Approach.

Example

#include <iostream>
#include <cmath>
using namespace std;

// Function for finding the sum of the powers of each digit
int sumOfPowers(int num) {
    int total = 0;
    int t = num;
    while (t > 0) {
        int val = t % 10;
        total+= pow(val, val);
        t /= 10;
    }
    return total;
}

//The brute force approach for finding the numbers
void findNumbersBruteForce(int s, int f) {
    cout << "The Munchausen numbers in the given range  [" << s << ", " << f << "] are:" << endl;
    for (int i = s; i <= f; ++i) {
        if (i == sumOfPowers(i))
            cout << i << endl;
    }
}

int main() {
    int initial, last;
    cout << "Please enter the range of numbers(initial and last) :";
    cin >> initial >> last;
    
    findNumbersBruteForce(initial,last);
    
    return 0;
}

Output:

Output

Please enter the range of numbers(initial and last) :1 100
The Munchausen numbers in the given range  [1, 100] are:
1

Explanation:

The program consists of 3 functions:

  1. sumOfPowers(int num):
  • This function requires a whole integer to be passed as num parameter.
  • num is the number it computes as follows: n=(digits[0]^0)+(digits[1]^1)+(digits[2]^2)+. While digits[n-1] ** (n-1) determines the risk associated with the parameter n.
  • We just follow a step-by-step approach here, which involves starting from the least significant digit and counting up one by one. The numbers of each digit give a compound number, which is multiplied by the pow The result of this power added to the sum is the sum itself. Calculates the sum of the powers.
  1. findNumbersBruteForce(int s, int f):
  • This function gets two parameters, s and f , where the first is the starting point, and the last one is the point of finishing of the range.
  • The generator function generates Munchausen pseudorandom integers from ab to bk.
  • It bridges the execution of environments from the world on a scale simulative.
  • Next, the sumOfPowers function finds the sum of the product of "each digit of every number to the power of its digit in the number".
  • The statement is printed when the conditions are matched.
  1. main:
  • The main function has been implemented, which is the initial section of the program.
  • It enables the user to enter the range of numbers by using the beginning and ending points as issues.
  • The user defines a range of numbers that the program will generate.
  • The primary task takes a range parameter that was sent from the other function as its option.
  • The findMunchausenNumbersBruteForce function will be used to detect Munchausen numbers.
  • After that, it will return an integer value of 0, meaning that the operation was successful.
  • Approach 2: Using Optimization

Calculating the exponents of the numbers in each addition will be a time-consuming task. Therefore, precomputing these values and storing them in an array will not decrease the algorithm's processing time. Nonetheless, we have established a lookup table containing all the necessary values for efficiently verifying number equality.

Example:

Let's consider an example to demonstrate the Munchhausen number concept in C++ by applying an Optimization Approach.

Example

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
// Function for finding the sum of the powers of each digit
int sumOfPowers(int num, const vector<int>&power) {
    int total = 0;
    int t = num;
    while (t > 0) {
        int val = t % 10;
        total+= power[val];
        t /= 10;
    }
    return total;
}

// the Efficient approach for finding the numbers
void findNumbersBruteForce(int s, int f) {
    cout << "The Munchausen numbers in the given range  [" << s << ", " << f << "] are:" << endl;
    vector<int> power(10);
    for (int i = 0; i <= 9; ++i)
        power[i] = pow(i, i);
       
    for (int i = s; i <= f; ++i) {
        if (i == sumOfPowers(i,power))
            cout << i << endl;
    }
}
int main() {
    int initial, last;
    cout << "Please enter the range of numbers(initial and last) :";
    cin >> initial >> last;
    
    findNumbersBruteForce(initial,last);
    
    return 0;
}

Output:

Output

Please enter the range of numbers(initial and last) :1 2000
The Munchausen numbers in the given range  [1, 2000] are:
1

Explanation:

The code contains 3 functions:

  1. sumOfPowers(int num, const vector<int>& power):
  • This task takes an integer number num as the input and a constancpp tutorialer variable power thacpp tutorials to the previously calculated powers of digits as the input.
  • It performs a sum on all digits that are in the number num.
  • After that, iteration goes through digits of 'num', using the powers vector to recover each pre-calculated power. It then adds it up to 'total_sum' and calculates the resulting total of powers.
  1. findNumbersBruteForce(int s, int f):
  • This function takes two parameters, s, and f, as arguments, which are the first and lascpp tutorials of the range, accordingly.
  • It produces Munchausen numbers between numbers of the specific range [s, f].
  • The function starts with creating a vector of size 10 that holds pre-calculated exponents of numbers using a loop.
  • The iteration is running from s to f, using every number from that s to this f.
  • For each number, it verifies whether the number is equal to the sum of the values of the digits using the sumOfPowers function.
  1. main:
  • The findMunchausenNumbers function is passed with the range to call the findMunchausenNumbersBruteForce function.
  • Conclusion:

In summary, the provided code includes two distinct C++ implementations for solving the Munchhausen problem. The primary discrepancy lies in their respective methodologies for accomplishing the objective. The brute force strategy involves iteratively performing a set of operations by examining each number within a specified range to identify those possessing the defined characteristic. Conversely, the enhanced approach involves precomputing the powers before utilizing the resulting values to calculate the sum of powers for the given numbers.

Both of these methods are equally efficient and should be implemented evenly; the size of the Munchhausen list is determined by the frequency of interval measurements. Streamlined processes have minimized redundant calculations, particularly when dealing with numerous measurements. Nonetheless, the brute force approach is most suitable for a limited dataset. It is essential to validate the input range and ensure users provide accurate values to obtain the right result. In essence, this approach enables individuals to explore Munchhausen numbers within specific or broader intervals, with the selection of the solution contingent upon the interval in question.

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