Digital root/Multiplicative digital root: Difference between revisions

Updated to work with Nim 1.4. Removed "newSeqWith" template which is available in standard module "sequtils".
(Added a picolisp solution.)
(Updated to work with Nim 1.4. Removed "newSeqWith" template which is available in standard module "sequtils".)
Line 1,917:
=={{header|Nim}}==
{{trans|Python}}
<lang nim>import strutils, futuresequtils, sugar
 
templateproc newSeqWithmdroot(lenn: int): tuple[mp, initmdr: expr): exprint] =
var result {.gensym.} = newSeq[type(init)](len)
for i in 0 .. <len:
result[i] = init
result
 
proc mdroot(n): tuple[mp, mdr: int] =
var mdr = @[n]
while mdr[mdr.high] > 9:
Line 1,933 ⟶ 1,927:
mdr.add n
(mdr.high, mdr[mdr.high])
 
for n in [123321, 7739, 893, 899998]:
echo align($n, 6)," ",mdroot(n)
echo ""
 
var table = newSeqWith(10, newSeq[int]())
for n in 0..int.high:
if table.map((x: seq[int]) => x.len).min >= 5: break
table[mdroot(n).mdr].add n
 
for mp, val in table:
echo mp, ": ", val[0..4]</lang>
 
{{out}}
<pre>123321 (mp: 3, mdr: 8)
Anonymous user