WeixiangYan/CodeTransOcean huggingface dataset:
Task: Create a two-dimensional array at runtime








VB: Module Program
    Sub Main()
        Console.WriteLine("Enter two space-delimited integers:")
        Dim input = Console.ReadLine().Split()
        Dim rows = Integer.Parse(input(0))
        Dim cols = Integer.Parse(input(1))

        
        Dim arr(rows - 1, cols - 1) As Integer

        arr(0, 0) = 2
        Console.WriteLine(arr(0, 0))
    End Sub
End Module



Fortran: PROGRAM Example

  IMPLICIT NONE
  INTEGER :: rows, columns, errcheck
  INTEGER, ALLOCATABLE :: array(:,:)

  WRITE(*,*) "Enter number of rows"
  READ(*,*) rows
  WRITE(*,*) "Enter number of columns"
  READ(*,*) columns
 
  ALLOCATE (array(rows,columns), STAT=errcheck) 
 
  array(1,1) = 42
 
  WRITE(*,*) array(1,1)
 
  DEALLOCATE (array, STAT=errcheck)

END PROGRAM Example
Task: Munchausen numbers



C#: Func<char, int> toInt = c => c-'0';

foreach (var i in Enumerable.Range(1,5000)
	.Where(n => n == n.ToString()
		.Sum(x => Math.Pow(toInt(x), toInt(x)))))
	Console.WriteLine(i);









Ada: with Ada.Text_IO;

procedure Munchausen is

   function Is_Munchausen (M : in Natural) return Boolean is
      Table : constant array (Character range '0' .. '9') of Natural :=
        (0**0, 1**1, 2**2, 3**3, 4**4,
         5**5, 6**6, 7**7, 8**8, 9**9);
      Image : constant String := M'Image;
      Sum   : Natural := 0;
   begin
      for I in Image'First + 1 .. Image'Last loop
         Sum := Sum + Table (Image (I));
      end loop;
      return Image = Sum'Image;
   end Is_Munchausen;

begin
   for M in 1 .. 5_000 loop
      if Is_Munchausen (M) then
         Ada.Text_IO.Put (M'Image);
      end if;
   end loop;
   Ada.Text_IO.New_Line;
end Munchausen;
Task: Munchausen numbers

C: #include <stdio.h>
#include <math.h>

int main() {
    for (int i = 1; i < 5000; i++) {
        
        
        int sum = 0;
        for (int number = i; number > 0; number /= 10) {
            int digit = number % 10;
            
            
            sum += pow(digit, digit);
        }
        if (sum == i) {
            
            
            
            printf("%i\n", i);
        } 
    }
    return 0;
}











Ada: with Ada.Text_IO;

procedure Munchausen is

   function Is_Munchausen (M : in Natural) return Boolean is
      Table : constant array (Character range '0' .. '9') of Natural :=
        (0**0, 1**1, 2**2, 3**3, 4**4,
         5**5, 6**6, 7**7, 8**8, 9**9);
      Image : constant String := M'Image;
      Sum   : Natural := 0;
   begin
      for I in Image'First + 1 .. Image'Last loop
         Sum := Sum + Table (Image (I));
      end loop;
      return Image = Sum'Image;
   end Is_Munchausen;

begin
   for M in 1 .. 5_000 loop
      if Is_Munchausen (M) then
         Ada.Text_IO.Put (M'Image);
      end if;
   end loop;
   Ada.Text_IO.New_Line;
end Munchausen;
Task: Munchausen numbers


C++: #include <math.h>
#include <iostream>

unsigned pwr[10];

unsigned munch( unsigned i ) {
    unsigned sum = 0;
    while( i ) {
        sum += pwr[(i % 10)];
        i /= 10;
    }
    return sum;
}

int main( int argc, char* argv[] ) {
    for( int i = 0; i < 10; i++ )
        pwr[i] = (unsigned)pow( (float)i, (float)i );
    std::cout << "Munchausen Numbers\n==================\n";
    for( unsigned i = 1; i < 5000; i++ )
        if( i == munch( i ) ) std::cout << i << "\n";
    return 0;
}










Ada: with Ada.Text_IO;

procedure Munchausen is

   function Is_Munchausen (M : in Natural) return Boolean is
      Table : constant array (Character range '0' .. '9') of Natural :=
        (0**0, 1**1, 2**2, 3**3, 4**4,
         5**5, 6**6, 7**7, 8**8, 9**9);
      Image : constant String := M'Image;
      Sum   : Natural := 0;
   begin
      for I in Image'First + 1 .. Image'Last loop
         Sum := Sum + Table (Image (I));
      end loop;
      return Image = Sum'Image;
   end Is_Munchausen;

begin
   for M in 1 .. 5_000 loop
      if Is_Munchausen (M) then
         Ada.Text_IO.Put (M'Image);
      end if;
   end loop;
   Ada.Text_IO.New_Line;
end Munchausen;
Task: Munchausen numbers




Go: package main

import(
    "fmt"
    "math"
)

var powers [10]int

func isMunchausen(n int) bool {
    if n < 0 { return false }
    n64 := int64(n)
    nn  := n64
    var sum int64 = 0
    for nn > 0 {
        sum += int64(powers[nn % 10])
        if sum > n64 { return false }
        nn /= 10
    }
    return sum == n64
}

func main() {
    
    for i := 1; i <= 9; i++ {
        d := float64(i)  
        powers[i] = int(math.Pow(d, d))
    }
 
    
    fmt.Println("The Munchausen numbers between 0 and 500 million are:")
    for i := 0; i <= 500000000; i++ {
        if isMunchausen(i) { fmt.Printf("%d ", i) }
    }
    fmt.Println()
}








Ada: with Ada.Text_IO;

procedure Munchausen is

   function Is_Munchausen (M : in Natural) return Boolean is
      Table : constant array (Character range '0' .. '9') of Natural :=
        (0**0, 1**1, 2**2, 3**3, 4**4,
         5**5, 6**6, 7**7, 8**8, 9**9);
      Image : constant String := M'Image;
      Sum   : Natural := 0;
   begin
      for I in Image'First + 1 .. Image'Last loop
         Sum := Sum + Table (Image (I));
      end loop;
      return Image = Sum'Image;
   end Is_Munchausen;

begin
   for M in 1 .. 5_000 loop
      if Is_Munchausen (M) then
         Ada.Text_IO.Put (M'Image);
      end if;
   end loop;
   Ada.Text_IO.New_Line;
end Munchausen;
Task: Munchausen numbers





Java: public class Main {
    public static void main(String[] args) {
        for(int i = 0 ; i <= 5000 ; i++ ){
            int val = String.valueOf(i).chars().map(x -> (int) Math.pow( x-48 ,x-48)).sum();
            if( i == val){
                System.out.println( i + " (munchausen)");
            }
        }
    }
}







Ada: with Ada.Text_IO;

procedure Munchausen is

   function Is_Munchausen (M : in Natural) return Boolean is
      Table : constant array (Character range '0' .. '9') of Natural :=
        (0**0, 1**1, 2**2, 3**3, 4**4,
         5**5, 6**6, 7**7, 8**8, 9**9);
      Image : constant String := M'Image;
      Sum   : Natural := 0;
   begin
      for I in Image'First + 1 .. Image'Last loop
         Sum := Sum + Table (Image (I));
      end loop;
      return Image = Sum'Image;
   end Is_Munchausen;

begin
   for M in 1 .. 5_000 loop
      if Is_Munchausen (M) then
         Ada.Text_IO.Put (M'Image);
      end if;
   end loop;
   Ada.Text_IO.New_Line;
end Munchausen;
Task: Munchausen numbers







Python: for i in range(5000):
    if i == sum(int(x) ** int(x) for x in str(i)):
        print(i)





Ada: with Ada.Text_IO;

procedure Munchausen is

   function Is_Munchausen (M : in Natural) return Boolean is
      Table : constant array (Character range '0' .. '9') of Natural :=
        (0**0, 1**1, 2**2, 3**3, 4**4,
         5**5, 6**6, 7**7, 8**8, 9**9);
      Image : constant String := M'Image;
      Sum   : Natural := 0;
   begin
      for I in Image'First + 1 .. Image'Last loop
         Sum := Sum + Table (Image (I));
      end loop;
      return Image = Sum'Image;
   end Is_Munchausen;

begin
   for M in 1 .. 5_000 loop
      if Is_Munchausen (M) then
         Ada.Text_IO.Put (M'Image);
      end if;
   end loop;
   Ada.Text_IO.New_Line;
end Munchausen;
Task: Munchausen numbers








VB: Option Explicit

Sub Main_Munchausen_numbers()
Dim i&

    For i = 1 To 5000
        If IsMunchausen(i) Then Debug.Print i & " is a munchausen number."
    Next i
End Sub

Function IsMunchausen(Number As Long) As Boolean
Dim Digits, i As Byte, Tot As Long

    Digits = Split(StrConv(Number, vbUnicode), Chr(0))
    For i = 0 To UBound(Digits) - 1
        Tot = (Digits(i) ^ Digits(i)) + Tot
    Next i
    IsMunchausen = (Tot = Number)
End Function




Ada: with Ada.Text_IO;

procedure Munchausen is

   function Is_Munchausen (M : in Natural) return Boolean is
      Table : constant array (Character range '0' .. '9') of Natural :=
        (0**0, 1**1, 2**2, 3**3, 4**4,
         5**5, 6**6, 7**7, 8**8, 9**9);
      Image : constant String := M'Image;
      Sum   : Natural := 0;
   begin
      for I in Image'First + 1 .. Image'Last loop
         Sum := Sum + Table (Image (I));
      end loop;
      return Image = Sum'Image;
   end Is_Munchausen;

begin
   for M in 1 .. 5_000 loop
      if Is_Munchausen (M) then
         Ada.Text_IO.Put (M'Image);
      end if;
   end loop;
   Ada.Text_IO.New_Line;
end Munchausen;
Task: Munchausen numbers

C: #include <stdio.h>
#include <math.h>

int main() {
    for (int i = 1; i < 5000; i++) {
        
        
        int sum = 0;
        for (int number = i; number > 0; number /= 10) {
            int digit = number % 10;
            
            
            sum += pow(digit, digit);
        }
        if (sum == i) {
            
            
            
            printf("%i\n", i);
        } 
    }
    return 0;
}
Task: Munchausen numbers



C#: Func<char, int> toInt = c => c-'0';

foreach (var i in Enumerable.Range(1,5000)
	.Where(n => n == n.ToString()
		.Sum(x => Math.Pow(toInt(x), toInt(x)))))
	Console.WriteLine(i);
Task: Munchausen numbers


C++: #include <math.h>
#include <iostream>

unsigned pwr[10];

unsigned munch( unsigned i ) {
    unsigned sum = 0;
    while( i ) {
        sum += pwr[(i % 10)];
        i /= 10;
    }
    return sum;
}

int main( int argc, char* argv[] ) {
    for( int i = 0; i < 10; i++ )
        pwr[i] = (unsigned)pow( (float)i, (float)i );
    std::cout << "Munchausen Numbers\n==================\n";
    for( unsigned i = 1; i < 5000; i++ )
        if( i == munch( i ) ) std::cout << i << "\n";
    return 0;
}
Task: Munchausen numbers





Java: public class Main {
    public static void main(String[] args) {
        for(int i = 0 ; i <= 5000 ; i++ ){
            int val = String.valueOf(i).chars().map(x -> (int) Math.pow( x-48 ,x-48)).sum();
            if( i == val){
                System.out.println( i + " (munchausen)");
            }
        }
    }
}
Task: Munchausen numbers







Python: for i in range(5000):
    if i == sum(int(x) ** int(x) for x in str(i)):
        print(i)
Task: Munchausen numbers








VB: Option Explicit

Sub Main_Munchausen_numbers()
Dim i&

    For i = 1 To 5000
        If IsMunchausen(i) Then Debug.Print i & " is a munchausen number."
    Next i
End Sub

Function IsMunchausen(Number As Long) As Boolean
Dim Digits, i As Byte, Tot As Long

    Digits = Split(StrConv(Number, vbUnicode), Chr(0))
    For i = 0 To UBound(Digits) - 1
        Tot = (Digits(i) ^ Digits(i)) + Tot
    Next i
    IsMunchausen = (Tot = Number)
End Function
Task: Munchausen numbers




Go: package main

import(
    "fmt"
    "math"
)

var powers [10]int

func isMunchausen(n int) bool {
    if n < 0 { return false }
    n64 := int64(n)
    nn  := n64
    var sum int64 = 0
    for nn > 0 {
        sum += int64(powers[nn % 10])
        if sum > n64 { return false }
        nn /= 10
    }
    return sum == n64
}

func main() {
    
    for i := 1; i <= 9; i++ {
        d := float64(i)  
        powers[i] = int(math.Pow(d, d))
    }
 
    
    fmt.Println("The Munchausen numbers between 0 and 500 million are:")
    for i := 0; i <= 500000000; i++ {
        if isMunchausen(i) { fmt.Printf("%d ", i) }
    }
    fmt.Println()
}
Task: Munchausen numbers



C#: Func<char, int> toInt = c => c-'0';

foreach (var i in Enumerable.Range(1,5000)
	.Where(n => n == n.ToString()
		.Sum(x => Math.Pow(toInt(x), toInt(x)))))
	Console.WriteLine(i);








Fortran: C MUNCHAUSEN NUMBERS - FORTRAN IV
      DO 2 I=1,5000
        IS=0
        II=I
        DO 1 J=1,4
          ID=10**(4-J)
          N=II/ID
          IR=MOD(II,ID)
          IF(N.NE.0) IS=IS+N**N
  1       II=IR
  2     IF(IS.EQ.I) WRITE(*,*) I
      END
Task: Munchausen numbers


C++: #include <math.h>
#include <iostream>

unsigned pwr[10];

unsigned munch( unsigned i ) {
    unsigned sum = 0;
    while( i ) {
        sum += pwr[(i % 10)];
        i /= 10;
    }
    return sum;
}

int main( int argc, char* argv[] ) {
    for( int i = 0; i < 10; i++ )
        pwr[i] = (unsigned)pow( (float)i, (float)i );
    std::cout << "Munchausen Numbers\n==================\n";
    for( unsigned i = 1; i < 5000; i++ )
        if( i == munch( i ) ) std::cout << i << "\n";
    return 0;
}









Fortran: C MUNCHAUSEN NUMBERS - FORTRAN IV
      DO 2 I=1,5000
        IS=0
        II=I
        DO 1 J=1,4
          ID=10**(4-J)
          N=II/ID
          IR=MOD(II,ID)
          IF(N.NE.0) IS=IS+N**N
  1       II=IR
  2     IF(IS.EQ.I) WRITE(*,*) I
      END
Task: Munchausen numbers

C: #include <stdio.h>
#include <math.h>

int main() {
    for (int i = 1; i < 5000; i++) {
        
        
        int sum = 0;
        for (int number = i; number > 0; number /= 10) {
            int digit = number % 10;
            
            
            sum += pow(digit, digit);
        }
        if (sum == i) {
            
            
            
            printf("%i\n", i);
        } 
    }
    return 0;
}










Fortran: C MUNCHAUSEN NUMBERS - FORTRAN IV
      DO 2 I=1,5000
        IS=0
        II=I
        DO 1 J=1,4
          ID=10**(4-J)
          N=II/ID
          IR=MOD(II,ID)
          IF(N.NE.0) IS=IS+N**N
  1       II=IR
  2     IF(IS.EQ.I) WRITE(*,*) I
      END
Task: Munchausen numbers




Go: package main

import(
    "fmt"
    "math"
)

var powers [10]int

func isMunchausen(n int) bool {
    if n < 0 { return false }
    n64 := int64(n)
    nn  := n64
    var sum int64 = 0
    for nn > 0 {
        sum += int64(powers[nn % 10])
        if sum > n64 { return false }
        nn /= 10
    }
    return sum == n64
}

func main() {
    
    for i := 1; i <= 9; i++ {
        d := float64(i)  
        powers[i] = int(math.Pow(d, d))
    }
 
    
    fmt.Println("The Munchausen numbers between 0 and 500 million are:")
    for i := 0; i <= 500000000; i++ {
        if isMunchausen(i) { fmt.Printf("%d ", i) }
    }
    fmt.Println()
}







Fortran: C MUNCHAUSEN NUMBERS - FORTRAN IV
      DO 2 I=1,5000
        IS=0
        II=I
        DO 1 J=1,4
          ID=10**(4-J)
          N=II/ID
          IR=MOD(II,ID)
          IF(N.NE.0) IS=IS+N**N
  1       II=IR
  2     IF(IS.EQ.I) WRITE(*,*) I
      END
Task: Munchausen numbers





Java: public class Main {
    public static void main(String[] args) {
        for(int i = 0 ; i <= 5000 ; i++ ){
            int val = String.valueOf(i).chars().map(x -> (int) Math.pow( x-48 ,x-48)).sum();
            if( i == val){
                System.out.println( i + " (munchausen)");
            }
        }
    }
}






Fortran: C MUNCHAUSEN NUMBERS - FORTRAN IV
      DO 2 I=1,5000
        IS=0
        II=I
        DO 1 J=1,4
          ID=10**(4-J)
          N=II/ID
          IR=MOD(II,ID)
          IF(N.NE.0) IS=IS+N**N
  1       II=IR
  2     IF(IS.EQ.I) WRITE(*,*) I
      END
Task: Munchausen numbers







Python: for i in range(5000):
    if i == sum(int(x) ** int(x) for x in str(i)):
        print(i)




Fortran: C MUNCHAUSEN NUMBERS - FORTRAN IV
      DO 2 I=1,5000
        IS=0
        II=I
        DO 1 J=1,4
          ID=10**(4-J)
          N=II/ID
          IR=MOD(II,ID)
          IF(N.NE.0) IS=IS+N**N
  1       II=IR
  2     IF(IS.EQ.I) WRITE(*,*) I
      END
Task: Munchausen numbers








VB: Option Explicit

Sub Main_Munchausen_numbers()
Dim i&

    For i = 1 To 5000
        If IsMunchausen(i) Then Debug.Print i & " is a munchausen number."
    Next i
End Sub

Function IsMunchausen(Number As Long) As Boolean
Dim Digits, i As Byte, Tot As Long

    Digits = Split(StrConv(Number, vbUnicode), Chr(0))
    For i = 0 To UBound(Digits) - 1
        Tot = (Digits(i) ^ Digits(i)) + Tot
    Next i
    IsMunchausen = (Tot = Number)
End Function



Fortran: C MUNCHAUSEN NUMBERS - FORTRAN IV
      DO 2 I=1,5000
        IS=0
        II=I
        DO 1 J=1,4
          ID=10**(4-J)
          N=II/ID
          IR=MOD(II,ID)
          IF(N.NE.0) IS=IS+N**N
  1       II=IR
  2     IF(IS.EQ.I) WRITE(*,*) I
      END
Task: Munchausen numbers






PHP: <?php 

$pwr = array_fill(0, 10, 0);

function isMunchhausen($n)
{
    global $pwr;
    $sm = 0;
    $temp = $n;
    while ($temp) {
        $sm= $sm + $pwr[($temp % 10)];
        $temp = (int)($temp / 10);
    }
    return $sm == $n;
}

for ($i = 0; $i < 10; $i++) {
    $pwr[$i] = pow((float)($i), (float)($i));
}

for ($i = 1; $i < 5000 + 1; $i++) {
    if (isMunchhausen($i)) {
        echo $i . PHP_EOL;
    }
}





Fortran: C MUNCHAUSEN NUMBERS - FORTRAN IV
      DO 2 I=1,5000
        IS=0
        II=I
        DO 1 J=1,4
          ID=10**(4-J)
          N=II/ID
          IR=MOD(II,ID)
          IF(N.NE.0) IS=IS+N**N
  1       II=IR
  2     IF(IS.EQ.I) WRITE(*,*) I
      END
Task: Munchausen numbers

C: #include <stdio.h>
#include <math.h>

int main() {
    for (int i = 1; i < 5000; i++) {
        
        
        int sum = 0;
        for (int number = i; number > 0; number /= 10) {
            int digit = number % 10;
            
            
            sum += pow(digit, digit);
        }
        if (sum == i) {
            
            
            
            printf("%i\n", i);
        } 
    }
    return 0;
}









COBOL:        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)
Task: Munchausen numbers



C#: Func<char, int> toInt = c => c-'0';

foreach (var i in Enumerable.Range(1,5000)
	.Where(n => n == n.ToString()
		.Sum(x => Math.Pow(toInt(x), toInt(x)))))
	Console.WriteLine(i);







COBOL:        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)
Task: Munchausen numbers


C++: #include <math.h>
#include <iostream>

unsigned pwr[10];

unsigned munch( unsigned i ) {
    unsigned sum = 0;
    while( i ) {
        sum += pwr[(i % 10)];
        i /= 10;
    }
    return sum;
}

int main( int argc, char* argv[] ) {
    for( int i = 0; i < 10; i++ )
        pwr[i] = (unsigned)pow( (float)i, (float)i );
    std::cout << "Munchausen Numbers\n==================\n";
    for( unsigned i = 1; i < 5000; i++ )
        if( i == munch( i ) ) std::cout << i << "\n";
    return 0;
}








COBOL:        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)
Task: Munchausen numbers





Java: public class Main {
    public static void main(String[] args) {
        for(int i = 0 ; i <= 5000 ; i++ ){
            int val = String.valueOf(i).chars().map(x -> (int) Math.pow( x-48 ,x-48)).sum();
            if( i == val){
                System.out.println( i + " (munchausen)");
            }
        }
    }
}





COBOL:        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)
Task: Munchausen numbers







Python: for i in range(5000):
    if i == sum(int(x) ** int(x) for x in str(i)):
        print(i)



COBOL:        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)