Digital root/Multiplicative digital root: Difference between revisions

no edit summary
m (→‎ultra-fast version: added/changed comments, whitespace, and indentations, optimized a function, used a template for output.)
No edit summary
Line 2,319:
8: [8 18 24 29 36 38 42 46 49 63 64 66 67 76 77 79 81 83 88 92 94 97 99 118 124 129 136 138 142 146 149 163 164 166]
9: [9 19 33 91 119 133 191 313 331 911 1119 1133 1191 1313 1331 1911 3113 3131 3311 9111 11119 11133 11191 11313 11331 11911 13113 13131 13311 19111 31113 31131 31311 33111]
</pre>
 
=={{header|Ring}}==
<lang ring>
# Project : Digital root/Multiplicative digital root
# Date : 2017/11/21
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
 
load "stdlib.ring"
root = newlist(10, 5)
for r = 1 to 10
for x = 1 to 5
root[r][x] = 0
next
next
root2 = list(10)
for y = 1 to 10
root2[y] = 0
next
see "Number MDR MP" + nl
num = [123321, 7739, 893, 899998]
digroot(num)
see nl
num = 0:12000
digroot(num)
see "First five numbers with MDR in first column:" + nl
for n1 = 1 to 10
see "" + (n1-1) + " => "
for n2 = 1 to 5
see "" + root[n1][n2] + " "
next
see nl
next
 
func digroot(num)
for n = 1 to len(num)
sum = 0
numold = num[n]
while true
pro = 1
strnum = string(numold)
for nr = 1 to len(strnum)
pro = pro * number(strnum[nr])
next
sum = sum + 1
numold = pro
numn = string(num[n])
sp = 6 - len(string(num[n]))
if sp > 0
for p = 1 to sp + 2
numn = " " + numn
next
ok
if len(string(numold)) = 1 and len(num) < 5
see "" + numn + " " + numold + " " + sum + nl
exit
ok
if len(string(numold)) = 1 and len(num) > 4
root2[numold+1] = root2[numold+1] + 1
if root2[numold+1] < 6
root[numold+1][root2[numold+1]] = num[n]
ok
exit
ok
end
next
</lang>
Output:
<pre>
Number MDR MP
123321 8 3
7739 8 3
893 2 3
899998 0 2
 
First five numbers with MDR in first column:
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>
 
2,468

edits