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

Published on 12 May 2024 09:40 PM

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

Source code in the action! programming language

;there are considered digits 0-5 because 6^6>5000
DEFINE MAXDIGIT="5"
INT ARRAY powers(MAXDIGIT+1)

INT FUNC Power(BYTE x)
  INT res
  BYTE i

  IF x=0 THEN RETURN (0) FI
  res=1
  FOR i=0 TO x-1
  DO
    res==*x
  OD
RETURN (res)

BYTE FUNC IsMunchausen(INT x)
  INT sum,tmp
  BYTE d

  tmp=x sum=0
  WHILE tmp#0
  DO
    d=tmp MOD 10
    IF d>MAXDIGIT THEN
      RETURN (0)
    FI
    sum==+powers(d)
    tmp==/10
  OD
  IF sum=x THEN
    RETURN (1)
  FI
RETURN (0)

PROC Main()
  INT i

  FOR i=0 TO MAXDIGIT
  DO
    powers(i)=Power(i)
  OD
  FOR i=1 TO 5000
  DO
    IF IsMunchausen(i) THEN
      PrintIE(i)
    FI
  OD  
RETURN

  

You may also check:How to resolve the algorithm Command-line arguments step by step in the Fortran programming language
You may also check:How to resolve the algorithm Reduced row echelon form step by step in the Scheme programming language
You may also check:How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Swift programming language
You may also check:How to resolve the algorithm 24 game step by step in the Koka programming language
You may also check:How to resolve the algorithm Runtime evaluation/In an environment step by step in the ALGOL 68 programming language