Digital root/Multiplicative digital root: Difference between revisions
Content added Content deleted
(Added a picolisp solution.) |
(Updated to work with Nim 1.4. Removed "newSeqWith" template which is available in standard module "sequtils".) |
||
Line 1,917: | Line 1,917: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<lang nim>import strutils, |
<lang nim>import strutils, sequtils, sugar |
||
proc mdroot(n: int): tuple[mp, mdr: int] = |
|||
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] |
var mdr = @[n] |
||
while mdr[mdr.high] > 9: |
while mdr[mdr.high] > 9: |
||
Line 1,933: | Line 1,927: | ||
mdr.add n |
mdr.add n |
||
(mdr.high, mdr[mdr.high]) |
(mdr.high, mdr[mdr.high]) |
||
for n in [123321, 7739, 893, 899998]: |
for n in [123321, 7739, 893, 899998]: |
||
echo align($n, 6)," ",mdroot(n) |
echo align($n, 6)," ",mdroot(n) |
||
echo "" |
echo "" |
||
var table = newSeqWith(10, newSeq[int]()) |
var table = newSeqWith(10, newSeq[int]()) |
||
for n in 0..int.high: |
for n in 0..int.high: |
||
if table.map((x: seq[int]) => x.len).min >= 5: break |
if table.map((x: seq[int]) => x.len).min >= 5: break |
||
table[mdroot(n).mdr].add n |
table[mdroot(n).mdr].add n |
||
for mp, val in table: |
for mp, val in table: |
||
echo mp,": ",val[0..4]</lang> |
echo mp, ": ", val[0..4]</lang> |
||
{{out}} |
{{out}} |
||
<pre>123321 (mp: 3, mdr: 8) |
<pre>123321 (mp: 3, mdr: 8) |