How to resolve the algorithm Digital root/Multiplicative digital root step by step in the Rust programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Digital root/Multiplicative digital root step by step in the Rust programming language

Table of Contents

Problem Statement

The multiplicative digital root (MDR) and multiplicative persistence (MP) of a number,

n

{\displaystyle n}

, is calculated rather like the Digital root except digits are multiplied instead of being added:

Show all output on this page. The Product of decimal digits of n page was redirected here, and had the following description The three existing entries for Phix, REXX, and Ring have been moved here, under ===Similar=== headings, feel free to match or ignore them.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Digital root/Multiplicative digital root step by step in the Rust programming language

Source code in the rust programming language

// Multiplicative digital root
fn mdroot(n: u32) -> (u32, u32) {
    let mut count = 0;
    let mut mdr = n; 
    while mdr > 9 {
        let mut m = mdr;
        let mut digits_mul = 1;
        while m > 0 {
            digits_mul *= m % 10;
            m /= 10;
        }
        mdr = digits_mul;
        count += 1;
    }
    return (count, mdr);
}

fn main() {
    println!("Number: (MP, MDR)\n======  =========");
    for n in [123321, 7739, 893, 899998] {
        println!("{:6}: {:?}", n, mdroot(n));
    }        
    let mut table = vec![vec![0_u32; 0]; 10];
    let mut n = 0;
    while table.iter().map(|row| row.len()).min().unwrap() < 5 {
        let (_, mdr) = mdroot(n);
        table[mdr as usize].push(n);
        n += 1;
    }
    println!("\nMDR     First 5 with matching MDR\n===     =========================");
    table.sort();
    for a in table {
        println!("{:2}: {:5}{:6}{:6}{:6}{:6}", a[0], a[0], a[1], a[2], a[3], a[4]);
    }
}


  

You may also check:How to resolve the algorithm Zebra puzzle step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Attractive numbers step by step in the Tcl programming language
You may also check:How to resolve the algorithm Legendre prime counting function step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Semiprime step by step in the ERRE programming language