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

Source code in the factor programming language

USING: arrays formatting fry io kernel lists lists.lazy math
math.text.utils prettyprint sequences ;
IN: rosetta-code.multiplicative-digital-root

: mdr ( n -- {persistence,root} )
    0 swap
    [ 1 digit-groups dup length 1 > ] [ product [ 1 + ] dip ] while
    dup empty? [ drop { 0 } ] when first 2array ;

: print-mdr ( n -- )
    dup [ 1array ] dip mdr append
    "%-12d has multiplicative persistence %d and MDR %d.\n"
    vprintf ;

: first5 ( n -- seq ) ! first 5 numbers with MDR of n
    0 lfrom swap '[ mdr second _ = ] lfilter 5 swap ltake list>array ;

: print-first5 ( i n -- )
    "%-5d" printf bl first5 [ "%-5d " printf ] each nl ;

: header ( -- )
    "MDR | First five numbers with that MDR" print
    "--------------------------------------" print ;

: first5-table ( -- )
    header 10 iota [ print-first5 ] each-index ;

: main ( -- )
    { 123321 7739 893 899998 } [ print-mdr ] each nl first5-table ;

MAIN: main


  

You may also check:How to resolve the algorithm Sort an integer array step by step in the Ring programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Tcl programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the Objeck programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the GAP programming language