Münchhausen Number

Given a number N, output all Munchhausen numbers from 1 to n.

Introduction : A Münchhausen number is a number equal to the sum of its digits raised to each digit’s power. It is similar to that of Narcissistic Number.

For example:
3435 = 33 + 44 + 33 + 55



One can also be considered as Münchhausen Number because when 1 raised to the power 1 is 1 itself.

Since, the number 3435 can be expressed as sum of each digits of the number when each digits of the numbers are raised to power equivalent to the digits itself i.e., ((3 raised to the power 3) + (4 raised to the power 4) + (3 raised to the power 3) + (5 raised to the power 5)) will give output to the same number i.e. 3435, then the number can be called as Münchhausen Number.

Example:

Input : 500
Output : 1
One is the only Münchhausen Number smaller
than or equal to 500.

Input : 5000
Output : 1  3435
1 and 3435 are the only Münchhausen Numbers smaller
than or equal to 5000.

We precompute i raised to power i for every possible digit i where i varies from 0 to 9. After precomputing these values, we traverse through all digits of every number smaller than equal to n and compute sum of digit raised to power digit.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for Münchhausen Number
#include <bits/stdc++.h>
using namespace std; 
  
// pwr[i] is going to store i raised to
// power i.
unsigned pwr[10];
  
// Function to check out whether
// the number is Münchhausen
// Number or not 
bool isMunchhausen(unsigned n) {
    unsigned sum = 0;
    int temp = n;
  
    while (temp) {
        sum += pwr[(temp % 10)];
        temp /= 10;
    }
  
    return (sum == n);
}
  
void printMunchhausenNumbers(int n)
{
    // Precompute i raised to power i for every i
    for (int i = 0; i < 10; i++ )
        pwr[i] = (unsigned)pow( (float)i, (float)i );
      
    // The input here is fixed i.e. it will
    // check up to n
    for (unsigned i = 1; i <= n; i++) 
  
        // check the integer for Münchhausen Number, 
        // if yes then print out the number
        if (isMunchhausen(i))
            cout << i << "\n";
}
  
// Driver Code
int main() {
    int n = 10000;
    printMunchhausenNumbers(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Munchhausen Number
  
import java.io.*;
import java.util.*;
  
class GFG {
// pwr[i] is going to store i raised to
// power i.
static long[] pwr;
   
// Function to check out whether
// the number is Munchhausen
// Number or not 
static Boolean isMunchhausen(int n) {
    long sum = 0l;
    int temp = n;
   
    while (temp>0) {
        int index= temp%10;
        sum =sum + pwr[index];
        temp /= 10;
    }
   
    return (sum == n);
}
   
static void printMunchhausenNumbers(int n)
{
    pwr= new long[10];
  
    // Precompute i raised to
    // power i for every i
    for (int i = 0; i < 10; i++ )
        pwr[i] = (long)Math.pow( (float)i, (float)i );
       
    // The input here is fixed i.e. it will
    // check up to n
    for (int i = 1; i <= n; i++) 
   
        // check the integer for Munchhausen Number, 
        // if yes then print out the number
        if (isMunchhausen(i)==true)
            System.out.println(i );
}
    public static void main (String[] args) {
    int n = 10000;
    printMunchhausenNumbers(n);
     }
}
// This code is contributed by Gitanjali.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code for
# Münchhausen Number
import math
  
# pwr[i] is going to 
# store i raised to
# power i.
pwr = [0] * 10
  
# Function to check out
# whether the number is
# Münchhausen Number or
# not 
def isMunchhausen(n) :
  
    sm = 0
    temp = n
  
    while (temp) :
        sm= sm + pwr[(temp % 10)]
        temp = temp // 10
      
    return (sm == n)
  
def printMunchhausenNumbers(n) :
  
    # Precompute i raised to
    # power i for every i
    for i in range(0, 10) :
        pwr[i] = math.pow((float)(i), (float)(i))
      
    # The input here is fixed 
    # i.e. it will check up to n
    for i in range(1,n+1) :
          
        # check the integer for
        # Münchhausen Number, if
        # yes then print out the 
        # number
        if (isMunchhausen(i)) :
            print( i )
  
  
# Driver Code
n = 10000
printMunchhausenNumbers(n)
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code for Munchhausen Number
using System;
  
class GFG {
  
    // pwr[i] is going to store i
    // raised to power i.
    static long[] pwr;
  
    // Function to check out whether
    // the number is Munchhausen
    // Number or not
    static bool isMunchhausen(int n)
    {
        long sum = 0;
        int temp = n;
  
        while (temp > 0) {
            int index = temp % 10;
            sum = sum + pwr[index];
            temp /= 10;
        }
  
        return (sum == n);
    }
  
    static void printMunchhausenNumbers(int n)
    {
        pwr = new long[10];
  
        // Precompute i raised to
        // power i for every i
        for (int i = 0; i < 10; i++)
            pwr[i] = (long)Math.Pow((float)i, (float)i);
  
        // The input here is fixed i.e.
        // it will check up to n
        for (int i = 1; i <= n; i++)
  
            // check the integer for Munchhausen Number,
            // if yes then print out the number
            if (isMunchhausen(i) == true)
                Console.WriteLine(i);
    }
      
    // Driver Code
    public static void Main()
    {
        int n = 10000;
        printMunchhausenNumbers(n);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code for Münchhausen Number
  
// pwr[i] is going to store i raised 
// to power i.
$pwr = array_fill(0, 10, 0);
  
// Function to check out whether the 
// number is Münchhausen Number or not 
function isMunchhausen($n)
{
    global $pwr;
    $sm = 0;
    $temp = $n;
  
    while ($temp)
    {
        $sm= $sm + $pwr[($temp % 10)];
        $temp = (int)($temp / 10);
    }
    return ($sm == $n);
}
  
function printMunchhausenNumbers($n)
{
    global $pwr;
      
    // Precompute i raised to power 
    // i for every i
    for ($i = 0; $i < 10; $i++)
        $pwr[$i] = pow((float)($i), (float)($i));
      
    // The input here is fixed i.e. it 
    // will check up to n
    for ($i = 1; $i < $n + 1; $i++)
          
        // check the integer for Münchhausen 
        // Number, if yes then print out the 
        // number
        if (isMunchhausen($i))
            print($i . "\n");
}
  
// Driver Code
$n = 10000;
printMunchhausenNumbers($n);
  
// This code is contributed by mits
?>

chevron_right



Output:

1
3435

Note : If the definition 0^0 = 0 is adopted, then there are exactly four Münchhausen numbers: 0, 1, 3435, and 438579088 [Source : MathWorld]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar