Jump to content

Digital root/Multiplicative digital root: Difference between revisions

added Factor
(added Factor)
Line 1,033:
8: [8, 18, 24, 29, 36]
9: [9, 19, 33, 91, 119]
</pre>
 
=={{header|Factor}}==
<lang factor>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</lang>
{{out}}
<pre>
123321 has multiplicative persistence 3 and MDR 8.
7739 has multiplicative persistence 3 and MDR 8.
893 has multiplicative persistence 3 and MDR 2.
899998 has multiplicative persistence 2 and MDR 0.
 
MDR | First five numbers with that MDR
--------------------------------------
0 0 10 20 25 30
1 1 11 111 1111 11111
2 2 12 21 26 34
3 3 13 31 113 131
4 4 14 22 27 39
5 5 15 35 51 53
6 6 16 23 28 32
7 7 17 71 117 171
8 8 18 24 29 36
9 9 19 33 91 119
</pre>
 
1,808

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.