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

Published on 12 May 2024 09:40 PM

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

Source code in the nim programming language

import math

for i in 1..<5000:
  var sum: int64 = 0
  var number = i
  while number > 0:
    var digit = number mod 10
    sum += digit ^ digit
    number = number div 10
  if sum == i:
    echo i


  

You may also check:How to resolve the algorithm Guess the number step by step in the OCaml programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the Java programming language
You may also check:How to resolve the algorithm Determine if only one instance is running step by step in the OCaml programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the K programming language
You may also check:How to resolve the algorithm Pick random element step by step in the Free Pascal programming language