How to resolve the algorithm Digital root/Multiplicative digital root step by step in the CLU 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 CLU 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 CLU programming language

Source code in the clu programming language

digits = iter (n: int) yields (int)
    while n>0 do
        yield(n//10)
        n := n/10
    end
end digits

mdr = proc (n: int) returns (int,int)
    i: int := 0
    while n>=10 do
        m: int := 1
        for d: int in digits(n) do
            m := m * d
        end
        n := m
        i := i+1
    end
    return (i,n)
end mdr

first_mdr = iter (target_mdr, n: int) yields (int)
    i: int := 0
    while n>0 do
        x, m: int := mdr(i)
        if m=target_mdr then 
            yield(i) 
            n := n -1
        end
        i := i+1
    end
end first_mdr

start_up = proc ()
    po: stream := stream$primary_output()
    nums: sequence[int] := sequence[int]$[123321, 7739, 893, 899998]
    
    stream$putl(po, "  N     MDR  MP")
    stream$putl(po, "======  ===  ==")
    for num: int in sequence[int]$elements(nums) do
        stream$putright(po, int$unparse(num), 6)
        stream$puts(po, " ")
        i, m: int := mdr(num)
        stream$putright(po, int$unparse(m), 3)
        stream$puts(po, "  ")
        stream$putright(po, int$unparse(i), 3)
        stream$putl(po, "")
    end
    
    stream$putl(po, "\nMDR: [n0..n4]")
    stream$putl(po, "===  ========")
    for dgt: int in int$from_to(0,9) do
        stream$putright(po, int$unparse(dgt), 3)
        stream$puts(po, ": ")
        for num: int in first_mdr(dgt, 5) do
            stream$puts(po, int$unparse(num) || " ")
        end
        stream$putl(po, "")
    end
end start_up

  

You may also check:How to resolve the algorithm 21 game step by step in the Raku programming language
You may also check:How to resolve the algorithm Sudoku step by step in the Scilab programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the OCaml programming language
You may also check:How to resolve the algorithm Modular inverse step by step in the jq programming language