MÃ ¼ nchhausen Number | power i . ; Function to check out whether the number is Munchhausen Number or not ; Precompute i raised to power i for every i ; The input here is fixed i . e . it will check up to n ; check the integer for Munchhausen Number , if yes then prvar out the number ; Driver code
var pwr ; function isMunchhausen ( n ) { var sum = 0 ; var temp = n ; while ( temp > 0 ) { var index = temp % 10 ; sum = sum + pwr [ index ] ; temp = parseInt ( temp / 10 ) ; } return ( sum == n ) ; } function printMunchhausenNumbers ( n ) { pwr = Array . from ( { length : 10 } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < 10 ; i ++ ) pwr [ i ] = Math . pow ( i , i ) ; for ( var i = 1 ; i <= n ; i ++ ) if ( isMunchhausen ( i ) == true ) document . write ( i + " " ) ; } var n = 10000 ; printMunchhausenNumbers ( n ) ;