How to resolve the algorithm Munchausen numbers step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Munchausen numbers step by step in the D 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 D programming language
Source code in the d programming language
import std.stdio;
void main() {
for (int i=1; i<5000; i++) {
// loop through each digit in i
// e.g. for 1000 we get 0, 0, 0, 1.
int sum = 0;
for (int number=i; number>0; number/=10) {
int digit = number % 10;
// find the sum of the digits
// raised to themselves
sum += digit ^^ digit;
}
if (sum == i) {
// the sum is equal to the number
// itself; thus it is a
// munchausen number
writeln(i);
}
}
}
You may also check:How to resolve the algorithm Two's complement step by step in the Perl programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the X86 Assembly programming language
You may also check:How to resolve the algorithm Empty directory step by step in the Lasso programming language
You may also check:How to resolve the algorithm Binary strings step by step in the J programming language
You may also check:How to resolve the algorithm Munching squares step by step in the OCaml programming language