How to resolve the algorithm Munchausen numbers step by step in the COBOL programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Munchausen numbers step by step in the COBOL 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 COBOL programming language
Source code in the cobol programming language
IDENTIFICATION DIVISION.
PROGRAM-ID. MUNCHAUSEN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 CANDIDATE PIC 9(4).
03 DIGITS PIC 9 OCCURS 4 TIMES, REDEFINES CANDIDATE.
03 DIGIT PIC 9.
03 POWER-SUM PIC 9(5).
01 OUTPUT-LINE.
03 OUT-NUM PIC ZZZ9.
PROCEDURE DIVISION.
BEGIN.
PERFORM MUNCHAUSEN-TEST VARYING CANDIDATE FROM 1 BY 1
UNTIL CANDIDATE IS GREATER THAN 6000.
STOP RUN.
MUNCHAUSEN-TEST.
MOVE ZERO TO POWER-SUM.
MOVE 1 TO DIGIT.
INSPECT CANDIDATE TALLYING DIGIT FOR LEADING '0'.
PERFORM ADD-DIGIT-POWER VARYING DIGIT FROM DIGIT BY 1
UNTIL DIGIT IS GREATER THAN 4.
IF POWER-SUM IS EQUAL TO CANDIDATE,
MOVE CANDIDATE TO OUT-NUM,
DISPLAY OUTPUT-LINE.
ADD-DIGIT-POWER.
COMPUTE POWER-SUM =
POWER-SUM + DIGITS(DIGIT) ** DIGITS(DIGIT)
You may also check:How to resolve the algorithm Quine step by step in the GAP programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the COBOL programming language
You may also check:How to resolve the algorithm Snake step by step in the JavaScript programming language
You may also check:How to resolve the algorithm EKG sequence convergence step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Active object step by step in the SuperCollider programming language