How to resolve the algorithm Munchausen numbers step by step in the Java programming language
How to resolve the algorithm Munchausen numbers step by step in the Java programming language
Table of Contents
Problem Statement
A Munchausen number is a natural number n the sum of whose digits (in base 10), each raised to the power of itself, equals n. (Munchausen is also spelled: Münchhausen.) For instance: 3435 = 33 + 44 + 33 + 55
Find all Munchausen numbers between 1 and 5000.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Munchausen numbers step by step in the Java programming language
First solution: The first solution is a straightforward implementation of the Munchhausen problem using a simple loop. It calculates the sum of the digits of each number raised to the power of the digit itself. If the sum equals the original number, it prints the number as a Munchhausen number.
Second Solution: The second solution uses a more efficient approach by pre-computing the values of the digits raised to their own powers and stores them in an array called cache. To determine if a number is a Munchhausen number, it iterates through the digits of the number, calculates the sum of the cached values of the digits, and checks if the sum is equal to the original number. The advantage of this approach is that it avoids the need to calculate the powers of the digits repeatedly.
Explanation of the Code:
First Solution:
- The outer loop iterates from 0 to 5000.
- For each number i, it calculates the sum of the digits of i raised to the power of the digit itself using the chars() and map() methods of the String class.
- It checks if the sum is equal to i, and if so, it prints i as a Munchhausen number.
Second Solution:
- The main method pre-computes the values of the digits raised to their own powers and stores them in the cache array.
- The outer loop iterates from 0 to 500,000,000.
- For each number n, it calls the isMunchhausen() method to check if it is a Munchhausen number.
- The isMunchhausen() method calculates the sum of the cached values of the digits of n.
- If the sum is equal to n, it returns true, indicating that n is a Munchhausen number. Otherwise, it returns false.
Both solutions correctly identify the Munchhausen numbers within their respective ranges. The second solution is more efficient, especially for larger numbers, due to the pre-computation of the cached values.
Source code in the java programming language
public class Main {
public static void main(String[] args) {
for(int i = 0 ; i <= 5000 ; i++ ){
int val = String.valueOf(i).chars().map(x -> (int) Math.pow( x-48 ,x-48)).sum();
if( i == val){
System.out.println( i + " (munchausen)");
}
}
}
}
public class Munchhausen {
static final long[] cache = new long[10];
public static void main(String[] args) {
// Allowing 0 ^ 0 to be 0
for (int i = 1; i < 10; i++) {
cache[i] = (long) Math.pow(i, i);
}
for (long i = 0L; i <= 500_000_000L; i++) {
if (isMunchhausen(i)) {
System.out.println(i);
}
}
}
private static boolean isMunchhausen(long n) {
long sum = 0, nn = n;
do {
sum += cache[(int)(nn % 10)];
if (sum > n) {
return false;
}
nn /= 10;
} while (nn > 0);
return sum == n;
}
}
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the FunL programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Pascal programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Frink programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the D programming language
You may also check:How to resolve the algorithm Abelian sandpile model step by step in the Delphi programming language