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

Published on 12 May 2024 09:40 PM

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

Source code in the rust programming language

fn main() {
    let mut solutions = Vec::new();

    for num in 1..5_000 {
        let power_sum = num.to_string()
            .chars()
            .map(|c| {
                let digit = c.to_digit(10).unwrap();
                (digit as f64).powi(digit as i32) as usize
            })
            .sum::<usize>();

        if power_sum == num {
            solutions.push(num);
        }
    }

    println!("Munchausen numbers below 5_000 : {:?}", solutions);
}


  

You may also check:How to resolve the algorithm Cuban primes step by step in the Factor programming language
You may also check:How to resolve the algorithm Check Machin-like formulas step by step in the Maxima programming language
You may also check:How to resolve the algorithm Append a record to the end of a text file step by step in the Batch File programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the C# programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the Racket programming language