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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

A Disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.

135 is a Disarium number: 11 + 32 + 53 == 1 + 9 + 125 == 135 There are a finite number of Disarium numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Disarium numbers step by step in the Rust programming language

Source code in the rust programming language

fn power(n: i32, exp: i32) -> i32 {
    let mut result = 1;
    for _i in 0..exp {
        result *= n;
    }
    return result;
}

fn is_disarium(num: i32) -> bool {
    let mut n = num;
    let mut sum = 0;
    let mut i = 1;
    let len = num.to_string().len();
    while n > 0 {
        sum += power(n % 10, len as i32 - i + 1);
        n /= 10;
        i += 1
    }
    return sum == num;
}


fn main() {
    let mut i = 0;
    let mut count = 0;
    while count <= 18 {
        if is_disarium(i) {
            print!("{} ", i);
            count += 1;
        }
        i += 1;
    }
    println!("{}", " ")
}


  

You may also check:How to resolve the algorithm Associative array/Creation step by step in the Wart programming language
You may also check:How to resolve the algorithm Man or boy test step by step in the Ada programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Swift programming language
You may also check:How to resolve the algorithm Sieve of Pritchard step by step in the C# programming language
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Fortran programming language