How to resolve the algorithm Munchausen numbers step by step in the ALGOL W programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Munchausen numbers step by step in the ALGOL W 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 ALGOL W programming language
Source code in the algol programming language
% Find Munchausen Numbers between 1 and 5000 %
% note that 6^6 is 46 656 so we only need to consider numbers consisting of 0 to 5 %
begin
% table of nth Powers - note 0^0 is 0 for Munchausen numbers, not 1 %
integer array nthPower( 0 :: 5 );
integer d1, d2, d3, d4, d1Part, d2Part, d3Part;
nthPower( 0 ) := 0; nthPower( 1 ) := 1;
nthPower( 2 ) := 2 * 2; nthPower( 3 ) := 3 * 3 * 3;
nthPower( 4 ) := 4 * 4 * 4 * 4; nthPower( 5 ) := 5 * 5 * 5 * 5 * 5;
d1 := d2 := d3 := d1Part := d2Part := d3Part := 0;
d4 := 1;
while d1 < 6 do begin
integer number, digitPowerSum;
number := d1Part + d2Part + d3Part + d4;
digitPowerSum := nthPower( d1 )
+ nthPower( d2 )
+ nthPower( d3 )
+ nthPower( d4 );
if digitPowerSum = number then begin
write( i_w := 1, number )
end;
d4 := d4 + 1;
if d4 > 5 then begin
d4 := 0;
d3 := d3 + 1;
d3Part := d3Part + 10;
if d3 > 5 then begin
d3 := 0;
d3Part := 0;
d2 := d2 + 1;
d2Part := d2Part + 100;
if d2 > 5 then begin
d2 := 0;
d2Part := 0;
d1 := d1 + 1;
d1Part := d1Part + 1000;
end
end
end
end
end.
You may also check:How to resolve the algorithm Text processing/2 step by step in the 11l programming language
You may also check:How to resolve the algorithm Empty program step by step in the Julia programming language
You may also check:How to resolve the algorithm Input loop step by step in the Forth programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the PL/I programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Nemerle programming language