Digital root/Multiplicative digital root: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
(Add Red)
Line 2,788: Line 2,788:
8 : 8 18 24 29 36
8 : 8 18 24 29 36
9 : 9 19 33 91 119</pre>
9 : 9 19 33 91 119</pre>

=={{header|Red}}==
<lang rebol>Red ["Multiplicative digital root"]

mdr: function [
"Returns a block containing the mdr and persistence of an integer"
n [integer!]
][
persistence: 0
while [n > 10][
product: 1
m: n
while [m > 0][
product: m % 10 * product
m: to-integer m / 10
]
persistence: persistence + 1
n: product
]
reduce [n persistence]
]

foreach n [123321 7739 893 899998][
result: mdr n
print [pad n 6 "has multiplicative persistence" result/2 "and MDR" result/1]
]

print [newline "First five numbers with MDR of"]

repeat i 10 [
prin rejoin [i - 1 ": "]
hits: n: 0
while [hits < 5][
if i - 1 = first mdr n [
prin pad n 5
hits: hits + 1
]
n: n + 1
]
prin newline
]</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

First five numbers with MDR of
0: 0 20 30 40 45
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>


=={{header|REXX}}==
=={{header|REXX}}==