How to resolve the algorithm Munchausen numbers step by step in the Cowgol programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Munchausen numbers step by step in the Cowgol 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 Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";

sub digitPowerSum(n: uint16): (sum: uint32) is
    var powers: uint32[10] := 
        {1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489};
        
    sum := 0;
    loop
        sum := sum + powers[(n % 10) as uint8];
        n := n / 10;
        if n == 0 then break; end if;
    end loop;
end sub;

var n: uint16 := 1;
while n < 5000 loop
    if n as uint32 == digitPowerSum(n) then
        print_i16(n);
        print_nl();
    end if;
    n := n + 1;
end loop;

  

You may also check:How to resolve the algorithm Kolakoski sequence step by step in the zkl programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the Maxima programming language
You may also check:How to resolve the algorithm S-expressions step by step in the 11l programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Square-free integers step by step in the 11l programming language