Digital root/Multiplicative digital root: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 42: Line 42:
=={{header|11l}}==
=={{header|11l}}==
{{trans|Python}}
{{trans|Python}}
<lang 11l>F mdroot(n)
<syntaxhighlight lang="11l">F mdroot(n)
V count = 0
V count = 0
V mdr = n
V mdr = n
Line 73: Line 73:
L(val) table
L(val) table
print(‘#2: ’.format(L.index), end' ‘’)
print(‘#2: ’.format(L.index), end' ‘’)
print(val[0.<5])</lang>
print(val[0.<5])</syntaxhighlight>


{{out}}
{{out}}
Line 102: Line 102:
The solution uses the Package "Generic_Root" from the additive digital roots [[http://rosettacode.org/wiki/Digital_root#Ada]].
The solution uses the Package "Generic_Root" from the additive digital roots [[http://rosettacode.org/wiki/Digital_root#Ada]].


<lang Ada>with Ada.Text_IO, Generic_Root; use Generic_Root;
<syntaxhighlight lang="ada">with Ada.Text_IO, Generic_Root; use Generic_Root;


procedure Multiplicative_Root is
procedure Multiplicative_Root is
Line 148: Line 148:
TIO.New_Line;
TIO.New_Line;
end loop;
end loop;
end Multiplicative_Root;</lang>
end Multiplicative_Root;</syntaxhighlight>


{{out}}
{{out}}
Line 172: Line 172:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # Multiplicative Digital Roots #
<syntaxhighlight lang="algol68">BEGIN # Multiplicative Digital Roots #
# structure to hold the results of calculating the digital root & persistence #
# structure to hold the results of calculating the digital root & persistence #
MODE DR = STRUCT( INT root, INT persistence );
MODE DR = STRUCT( INT root, INT persistence );
Line 241: Line 241:
print md root( 899998 );
print md root( 899998 );
tabulate mdr( 5 )
tabulate mdr( 5 )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 263: Line 263:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
% calculate the Multiplicative Digital Root (mdr) and Multiplicative Persistence (mp) of n %
% calculate the Multiplicative Digital Root (mdr) and Multiplicative Persistence (mp) of n %
procedure getMDR ( integer value n
procedure getMDR ( integer value n
Line 328: Line 328:
end
end


end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 351: Line 351:


=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK># Multiplicative Digital Roots
<syntaxhighlight lang="awk"># Multiplicative Digital Roots


BEGIN {
BEGIN {
Line 426: Line 426:
} # for pos
} # for pos


} # tabulateMdr</lang>
} # tabulateMdr</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 448: Line 448:


=={{header|Bracmat}}==
=={{header|Bracmat}}==
<lang bracmat>(
<syntaxhighlight lang="bracmat">(
& ( MP/MDR
& ( MP/MDR
= prod L n
= prod L n
Line 493: Line 493:
& put$\n
& put$\n
)
)
);</lang>
);</syntaxhighlight>
{{out}}
{{out}}
<pre>123321 : (3.8)
<pre>123321 : (3.8)
Line 511: Line 511:


=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
#include <stdio.h>


Line 570: Line 570:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 593: Line 593:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 631: Line 631:
Console.WriteLine(" {0} : [{1}]", i, string.Join(", ", table[i]));
Console.WriteLine(" {0} : [{1}]", i, string.Join(", ", table[i]));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>123321 has multiplicative persistence 3 and multiplicative digital root 8
<pre>123321 has multiplicative persistence 3 and multiplicative digital root 8
Line 649: Line 649:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
<syntaxhighlight lang="cpp">
#include <iomanip>
#include <iomanip>
#include <map>
#include <map>
Line 709: Line 709:
return system( "pause" );
return system( "pause" );
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 736: Line 736:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>digits = iter (n: int) yields (int)
<syntaxhighlight lang="clu">digits = iter (n: int) yields (int)
while n>0 do
while n>0 do
yield(n//10)
yield(n//10)
Line 794: Line 794:
stream$putl(po, "")
stream$putl(po, "")
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre> N MDR MP
<pre> N MDR MP
Line 817: Line 817:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>
<syntaxhighlight lang="lisp">
(defun mdr/p (n)
(defun mdr/p (n)
"Return a list with MDR and MP of n"
"Return a list with MDR and MP of n"
Line 842: Line 842:
do (format t "~6@a: ~{~3@a ~}~%" el (mdr/p el)))
do (format t "~6@a: ~{~3@a ~}~%" el (mdr/p el)))
(format t "~%MDR: [n0..n4]~%")
(format t "~%MDR: [n0..n4]~%")
(first-n-number-for-each-root 5))</lang>
(first-n-number-for-each-root 5))</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 865: Line 865:
=={{header|Component Pascal}}==
=={{header|Component Pascal}}==
{{Works with| BlackBox Component Builder}}
{{Works with| BlackBox Component Builder}}
<lang oberon2>
<syntaxhighlight lang="oberon2">
MODULE MDR;
MODULE MDR;
IMPORT StdLog, Strings, TextMappers, DevCommanders;
IMPORT StdLog, Strings, TextMappers, DevCommanders;
Line 935: Line 935:


END MDR.
END MDR.
</syntaxhighlight>
</lang>
Execute:
Execute:
^Q MDR.Do 123321 7739 893 899998 ~
^Q MDR.Do 123321 7739 893 899998 ~
Line 963: Line 963:
=={{header|D}}==
=={{header|D}}==
{{trans|Python}}
{{trans|Python}}
<lang d>import std.stdio, std.algorithm, std.typecons, std.range, std.conv;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.typecons, std.range, std.conv;


/// Multiplicative digital root.
/// Multiplicative digital root.
Line 989: Line 989:
foreach (const mp; table.byKey.array.sort())
foreach (const mp; table.byKey.array.sort())
writefln("%2d: %s", mp, table[mp].take(5));
writefln("%2d: %s", mp, table[mp].take(5));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Number: (MP, MDR)
<pre>Number: (MP, MDR)
Line 1,012: Line 1,012:


===Alternative Version===
===Alternative Version===
<lang d>import std.stdio, std.algorithm, std.typecons, std.range;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.typecons, std.range;


uint digitsProduct(uint n) pure nothrow @nogc {
uint digitsProduct(uint n) pure nothrow @nogc {
Line 1,045: Line 1,045:
foreach (const mp; table.byKey.array.sort())
foreach (const mp; table.byKey.array.sort())
writefln("%2d: %s", mp, table[mp].take(5));
writefln("%2d: %s", mp, table[mp].take(5));
}</lang>
}</syntaxhighlight>


===More Efficient Version===
===More Efficient Version===
<lang d>import std.stdio, std.algorithm, std.range;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range;


/// Multiplicative digital root.
/// Multiplicative digital root.
Line 1,083: Line 1,083:
foreach (const mp; table.byKey.array.sort())
foreach (const mp; table.byKey.array.sort())
writefln("%2d: %s", mp, table[mp].take(5));
writefln("%2d: %s", mp, table[mp].take(5));
}</lang>
}</syntaxhighlight>
The output is similar.
The output is similar.


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule Digital do
<syntaxhighlight lang="elixir">defmodule Digital do
def mdroot(n), do: mdroot(n, 0)
def mdroot(n), do: mdroot(n, 0)
Line 1,123: Line 1,123:


Digital.task1([123321, 7739, 893, 899998])
Digital.task1([123321, 7739, 893, 899998])
Digital.task2</lang>
Digital.task2</syntaxhighlight>


{{out}}
{{out}}
Line 1,149: Line 1,149:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// mdr. Nigel Galloway: June 29th., 2021
// mdr. Nigel Galloway: June 29th., 2021
let rec fG n g=if n=0 then g else fG(n/10)(g*(n%10))
let rec fG n g=if n=0 then g else fG(n/10)(g*(n%10))
Line 1,156: Line 1,156:
let fN g=Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)g))|>Seq.take 5
let fN g=Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)g))|>Seq.take 5
seq{0..9}|>Seq.iter(fun n->printf "First 5 numbers with mdr %d -> " n; Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)n))|>Seq.take 5|>Seq.iter(printf "%d ");printfn "")
seq{0..9}|>Seq.iter(fun n->printf "First 5 numbers with mdr %d -> " n; Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)n))|>Seq.take 5|>Seq.iter(printf "%d ");printfn "")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,175: Line 1,175:
</pre>
</pre>
=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: arrays formatting fry io kernel lists lists.lazy math
<syntaxhighlight lang="factor">USING: arrays formatting fry io kernel lists lists.lazy math
math.text.utils prettyprint sequences ;
math.text.utils prettyprint sequences ;
IN: rosetta-code.multiplicative-digital-root
IN: rosetta-code.multiplicative-digital-root
Line 1,205: Line 1,205:
{ 123321 7739 893 899998 } [ print-mdr ] each nl first5-table ;
{ 123321 7739 893 899998 } [ print-mdr ] each nl first5-table ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,228: Line 1,228:


=={{header|Fortran}}==
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
<lang Fortran>
!Implemented by Anant Dixit (Oct, 2014)
!Implemented by Anant Dixit (Oct, 2014)
program mdr
program mdr
Line 1,320: Line 1,320:
end subroutine
end subroutine


</syntaxhighlight>
</lang>


<pre>
<pre>
Line 1,347: Line 1,347:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Function multDigitalRoot(n As UInteger, ByRef mp As Integer, base_ As Integer = 10) As Integer
Function multDigitalRoot(n As UInteger, ByRef mp As Integer, base_ As Integer = 10) As Integer
Line 1,398: Line 1,398:
Print
Print
Print "Press any key to quit"
Print "Press any key to quit"
Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 1,427: Line 1,427:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 1,481: Line 1,481:
fmt.Printf(tableFmt, i, l)
fmt.Printf(tableFmt, i, l)
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,508: Line 1,508:
=={{header|Haskell}}==
=={{header|Haskell}}==
Note that in the function <code>mdrNums</code> we don't know in advance how many numbers we'll need to examine to find the first 5 associated with all the MDRs. Using a lazy array to accumulate these numbers allows us to keep the function simple.
Note that in the function <code>mdrNums</code> we don't know in advance how many numbers we'll need to examine to find the first 5 associated with all the MDRs. Using a lazy array to accumulate these numbers allows us to keep the function simple.
<lang haskell>import Control.Arrow
<syntaxhighlight lang="haskell">import Control.Arrow
import Data.Array
import Data.Array
import Data.LazyArray
import Data.LazyArray
Line 1,548: Line 1,548:
printMpMdrs [123321, 7739, 893, 899998]
printMpMdrs [123321, 7739, 893, 899998]
putStrLn ""
putStrLn ""
printMdrNums 5</lang>
printMdrNums 5</syntaxhighlight>
{{out}}
{{out}}
Note that the values in the first column of the table are MDRs, as shown in the task's sample output, not MP as incorrectly stated in the task statement and column header.
Note that the values in the first column of the table are MDRs, as shown in the task's sample output, not MP as incorrectly stated in the task statement and column header.
Line 1,574: Line 1,574:


Works in both languages:
Works in both languages:
<lang unicon>procedure main(A)
<syntaxhighlight lang="unicon">procedure main(A)
write(right("n",8)," ",right("MP",8),right("MDR",5))
write(right("n",8)," ",right("MP",8),right("MDR",5))
every r := mdr(n := 123321|7739|893|899998) do
every r := mdr(n := 123321|7739|893|899998) do
Line 1,597: Line 1,597:
while m > 0 do c *:= 1(m%10, m/:=10)
while m > 0 do c *:= 1(m%10, m/:=10)
return c
return c
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,626: Line 1,626:
First, we need something to split a number into digits:
First, we need something to split a number into digits:


<lang J> 10&#.inv 123321
<syntaxhighlight lang="j"> 10&#.inv 123321
1 2 3 3 2 1</lang>
1 2 3 3 2 1</syntaxhighlight>


Second, we need to find their product:
Second, we need to find their product:


<lang J> */@(10&#.inv) 123321
<syntaxhighlight lang="j"> */@(10&#.inv) 123321
36</lang>
36</syntaxhighlight>


Then we use this inductively until it converges:
Then we use this inductively until it converges:


<lang J> */@(10&#.inv)^:a: 123321
<syntaxhighlight lang="j"> */@(10&#.inv)^:a: 123321
123321 36 18 8</lang>
123321 36 18 8</syntaxhighlight>


MP is one less than the length of this list, and MDR is the last element of this list:
MP is one less than the length of this list, and MDR is the last element of this list:


<lang J> (<:@#,{:) */@(10&#.inv)^:a: 123321
<syntaxhighlight lang="j"> (<:@#,{:) */@(10&#.inv)^:a: 123321
3 8
3 8
(<:@#,{:) */@(10&#.inv)^:a: 7739
(<:@#,{:) */@(10&#.inv)^:a: 7739
Line 1,648: Line 1,648:
3 2
3 2
(<:@#,{:) */@(10&#.inv)^:a: 899998
(<:@#,{:) */@(10&#.inv)^:a: 899998
2 0</lang>
2 0</syntaxhighlight>


For the table, we don't need that whole list, we only need the final value. Then use these values to classify the original argument (taking the first five from each group):
For the table, we don't need that whole list, we only need the final value. Then use these values to classify the original argument (taking the first five from each group):


<lang J> (5&{./.~ (*/@(10&#.inv)^:_)"0) i.20000
<syntaxhighlight lang="j"> (5&{./.~ (*/@(10&#.inv)^:_)"0) i.20000
0 10 20 25 30
0 10 20 25 30
1 11 111 1111 11111
1 11 111 1111 11111
Line 1,662: Line 1,662:
7 17 71 117 171
7 17 71 117 171
8 18 24 29 36
8 18 24 29 36
9 19 33 91 119</lang>
9 19 33 91 119</syntaxhighlight>


Note that since the first 10 non-negative integers are single digit values, the first column here doubles as a label (representing the corresponding multiplicative digital root).
Note that since the first 10 non-negative integers are single digit values, the first column here doubles as a label (representing the corresponding multiplicative digital root).
Line 1,668: Line 1,668:
=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|8}}
{{works with|Java|8}}
<lang java>import java.util.*;
<syntaxhighlight lang="java">import java.util.*;


public class MultiplicativeDigitalRoot {
public class MultiplicativeDigitalRoot {
Line 1,718: Line 1,718:
return new long[]{n, mdr, mp};
return new long[]{n, mdr, mp};
}
}
}</lang>
}</syntaxhighlight>


<pre>NUMBER MDR MP
<pre>NUMBER MDR MP
Line 1,739: Line 1,739:


=={{header|jq}}==
=={{header|jq}}==
<lang jq>def do_until(condition; next):
<syntaxhighlight lang="jq">def do_until(condition; next):
def u: if condition then . else (next|u) end;
def u: if condition then . else (next|u) end;
u;
u;
Line 1,770: Line 1,770:
end;
end;


[[], 0] | tab;</lang>
[[], 0] | tab;</syntaxhighlight>
'''Example''':<lang jq>
'''Example''':<syntaxhighlight lang="jq">
def neatly:
def neatly:
. as $in
. as $in
Line 1,786: Line 1,786:
"Tabulation",
"Tabulation",
"MDR: [n0..n4]",
"MDR: [n0..n4]",
(tabulate(5) | neatly)</lang>
(tabulate(5) | neatly)</syntaxhighlight>
{{out}}
{{out}}
<lang sh>$ jq -n -r -c -f mdr.jq
<syntaxhighlight lang="sh">$ jq -n -r -c -f mdr.jq


i : [MDR, MP]
i : [MDR, MP]
Line 1,807: Line 1,807:
7: [7,17,71,117,171]
7: [7,17,71,117,171]
8: [8,18,24,29,36]
8: [8,18,24,29,36]
9: [9,19,33,91,119]</lang>
9: [9,19,33,91,119]</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
'''Function'''
'''Function'''
<syntaxhighlight lang="julia">
<lang Julia>
function digitalmultroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
function digitalmultroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
-1 < n && 1 < bs || throw(DomainError())
-1 < n && 1 < bs || throw(DomainError())
Line 1,822: Line 1,822:
return (pers, ds)
return (pers, ds)
end
end
</syntaxhighlight>
</lang>
'''Main'''
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
const bs = 10
const bs = 10
const excnt = 5
const excnt = 5
Line 1,859: Line 1,859:
println(join([@sprintf("%6d", dmr[i, j]) for j in 1:excnt], ","))
println(join([@sprintf("%6d", dmr[i, j]) for j in 1:excnt], ","))
end
end
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,885: Line 1,885:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


fun multDigitalRoot(n: Int): Pair<Int, Int> = when {
fun multDigitalRoot(n: Int): Pair<Int, Int> = when {
Line 1,935: Line 1,935:
println()
println()
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,959: Line 1,959:
=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang M2000 Interpreter>multDigitalRoot=lambda (n as decimal) ->{
<syntaxhighlight lang="m2000 interpreter">multDigitalRoot=lambda (n as decimal) ->{
if n<0 then error "Negative numbers not allowed"
if n<0 then error "Negative numbers not allowed"
def decimal mdr, mp, nn
def decimal mdr, mp, nn
Line 2,010: Line 2,010:
Print #-2, doc$
Print #-2, doc$


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre> 123321 mdr = 8 MP = 3
<pre> 123321 mdr = 8 MP = 3
Line 2,030: Line 2,030:
</pre>
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang mathematica>
<syntaxhighlight lang="mathematica">
ClearAll[mdr, mp, nums];
ClearAll[mdr, mp, nums];
mdr[n_] := NestWhile[Times @@ IntegerDigits[#] &, n, # > 9 &];
mdr[n_] := NestWhile[Times @@ IntegerDigits[#] &, n, # > 9 &];
Line 2,040: Line 2,040:
TableForm[Table[{i, Take[nums[[i + 1]], 5]}, {i, 0, 9}],
TableForm[Table[{i, Take[nums[[i + 1]], 5]}, {i, 0, 9}],
TableHeadings -> {None, {"MDR", "First 5"}}, TableDepth -> 2]
TableHeadings -> {None, {"MDR", "First 5"}}, TableDepth -> 2]
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,067: Line 2,067:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Python}}
{{trans|Python}}
<lang nim>import strutils, sequtils, sugar
<syntaxhighlight lang="nim">import strutils, sequtils, sugar
proc mdroot(n: int): tuple[mp, mdr: int] =
proc mdroot(n: int): tuple[mp, mdr: int] =
Line 2,088: Line 2,088:
for mp, val in table:
for mp, val in table:
echo mp, ": ", val[0..4]</lang>
echo mp, ": ", val[0..4]</syntaxhighlight>


{{out}}
{{out}}
Line 2,108: Line 2,108:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>a(n)=my(i);while(n>9,n=factorback(digits(n));i++);[i,n];
<syntaxhighlight lang="parigp">a(n)=my(i);while(n>9,n=factorback(digits(n));i++);[i,n];
apply(a, [123321, 7739, 893, 899998])
apply(a, [123321, 7739, 893, 899998])
v=vector(10,i,[]); forstep(n=0,oo,1, t=a(n)[2]+1; if(#v[t]<5,v[t]=concat(v[t],n); if(vecmin(apply(length,v))>4, return(v))))</lang>
v=vector(10,i,[]); forstep(n=0,oo,1, t=a(n)[2]+1; if(#v[t]<5,v[t]=concat(v[t],n); if(vecmin(apply(length,v))>4, return(v))))</syntaxhighlight>
{{out}}
{{out}}
<pre>%1 = [[3, 8], [3, 8], [3, 2], [2, 0]]
<pre>%1 = [[3, 8], [3, 8], [3, 2], [2, 0]]
Line 2,118: Line 2,118:
inspired by [[Worthwhile_task_shaving]] :-)<BR>
inspired by [[Worthwhile_task_shaving]] :-)<BR>
Brute force speed up GetMulDigits.
Brute force speed up GetMulDigits.
<lang pascal>program MultRoot;
<syntaxhighlight lang="pascal">program MultRoot;
{$IFDEF FPC}
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$CODEALIGN proc=16}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$CODEALIGN proc=16}
Line 2,244: Line 2,244:
readln;
readln;
{$ENDIF}
{$ENDIF}
END.</lang>
END.</syntaxhighlight>
{{out|@TIO.RUN}}
{{out|@TIO.RUN}}
<pre>
<pre>
Line 2,281: Line 2,281:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|D}}
{{trans|D}}
<lang Perl>use warnings;
<syntaxhighlight lang="perl">use warnings;
use strict;
use strict;


Line 2,308: Line 2,308:
my @n = map { $i++ while (mdr($i))[1] != $target; $i++; } 1..5;
my @n = map { $i++ while (mdr($i))[1] != $target; $i++; } 1..5;
print " $target: [", join(", ", @n), "]\n";
print " $target: [", join(", ", @n), "]\n";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Number: (MP, MDR)
<pre>Number: (MP, MDR)
Line 2,332: Line 2,332:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang picolisp>(de mdr-mp (N)
<syntaxhighlight lang="picolisp">(de mdr-mp (N)
"Returns the solutions in a list, i.e., '(MDR MP)"
"Returns the solutions in a list, i.e., '(MDR MP)"
(let MP 0
(let MP 0
Line 2,367: Line 2,367:
(tab Fmt "===" " " "======")
(tab Fmt "===" " " "======")
(for (I . S) *Solutions
(for (I . S) *Solutions
(tab Fmt (dec I) ": " (glue ", " S)) ) )</lang>
(tab Fmt (dec I) ": " (glue ", " S)) ) )</syntaxhighlight>


{{out}}
{{out}}
Line 2,391: Line 2,391:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mdr_mp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mdr_mp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
Line 2,436: Line 2,436:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d %5d %5d %5d %5d %5d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d %5d %5d %5d %5d %5d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,461: Line 2,461:


===Similar===
===Similar===
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pdd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pdd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Product of the decimal digits of 1..100:\n%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pdd</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Product of the decimal digits of 1..100:\n%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pdd</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,484: Line 2,484:
===version 1===
===version 1===
{{incomplete|PL/I|Missing second half of task!}}
{{incomplete|PL/I|Missing second half of task!}}
<lang pli>multiple: procedure options (main); /* 29 April 2014 */
<syntaxhighlight lang="pli">multiple: procedure options (main); /* 29 April 2014 */


declare n fixed binary (31);
declare n fixed binary (31);
Line 2,507: Line 2,507:
end;
end;


end multiple;</lang>
end multiple;</syntaxhighlight>
{{out}}
{{out}}
<pre>N= 123321 MDR= 8 MP= 3;
<pre>N= 123321 MDR= 8 MP= 3;
Line 2,515: Line 2,515:


===version 2===
===version 2===
<lang pli> mdrt: Proc Options(main);
<syntaxhighlight lang="pli"> mdrt: Proc Options(main);
Dcl (x,p,r) Bin Fixed(31);
Dcl (x,p,r) Bin Fixed(31);
Put Edit('number persistence multiplicative digital root')(Skip,a);
Put Edit('number persistence multiplicative digital root')(Skip,a);
Line 2,570: Line 2,570:
End;
End;
End;
End;
End;</lang>
End;</syntaxhighlight>
{{out}}
{{out}}
<pre>number persistence multiplicative digital root
<pre>number persistence multiplicative digital root
Line 2,594: Line 2,594:
=={{header|Python}}==
=={{header|Python}}==
===Python: Inspired by the solution to the [[Digital root#Python|Digital root]] task===
===Python: Inspired by the solution to the [[Digital root#Python|Digital root]] task===
<lang python>try:
<syntaxhighlight lang="python">try:
from functools import reduce
from functools import reduce
except:
except:
Line 2,618: Line 2,618:
print('\nMP: [n0..n4]\n== ========')
print('\nMP: [n0..n4]\n== ========')
for mp, val in sorted(table.items()):
for mp, val in sorted(table.items()):
print('%2i: %r' % (mp, val[:5]))</lang>
print('%2i: %r' % (mp, val[:5]))</syntaxhighlight>


{{out}}
{{out}}
Line 2,643: Line 2,643:
===Python: Inspired by the [[Digital_root/Multiplicative_digital_root#More_Efficient_Version|more efficient version of D]].===
===Python: Inspired by the [[Digital_root/Multiplicative_digital_root#More_Efficient_Version|more efficient version of D]].===
Substitute the following function to run twice as fast when calculating mdroot(n) with n in range(1000000).
Substitute the following function to run twice as fast when calculating mdroot(n) with n in range(1000000).
<lang python>def mdroot(n):
<syntaxhighlight lang="python">def mdroot(n):
count, mdr = 0, n
count, mdr = 0, n
while mdr > 9:
while mdr > 9:
Line 2,652: Line 2,652:
mdr = digitsMul
mdr = digitsMul
count += 1
count += 1
return count, mdr</lang>
return count, mdr</syntaxhighlight>


{{out}}
{{out}}
Line 2,659: Line 2,659:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ abs 1 swap
<syntaxhighlight lang="quackery"> [ abs 1 swap
[ base share /mod
[ base share /mod
rot * swap
rot * swap
Line 2,692: Line 2,692:
' [ 123321 7739 893 899998 ] witheach task.1
' [ 123321 7739 893 899998 ] witheach task.1
cr
cr
10 task.2</lang>
10 task.2</syntaxhighlight>


{{out}}
{{out}}
Line 2,714: Line 2,714:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define (digital-product n)
(define (digital-product n)
(define (inr-d-p m rv)
(define (inr-d-p m rv)
Line 2,736: Line 2,736:
(for ((MDR (in-range 10)))
(for ((MDR (in-range 10)))
(define (has-mdr? n) (define-values (mdr mp) (mdr/mp n)) (= mdr MDR))
(define (has-mdr? n) (define-values (mdr mp) (mdr/mp n)) (= mdr MDR))
(printf "~a\t~a~%" MDR (for/list ((_ 5) (n (sequence-filter has-mdr? (in-naturals)))) n)))</lang>
(printf "~a\t~a~%" MDR (for/list ((_ 5) (n (sequence-filter has-mdr? (in-naturals)))) n)))</syntaxhighlight>
{{out}}
{{out}}
<pre>Number MDR mp
<pre>Number MDR mp
Line 2,760: Line 2,760:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>sub multiplicative-digital-root(Int $n) {
<syntaxhighlight lang="raku" line>sub multiplicative-digital-root(Int $n) {
return .elems - 1, .[.end]
return .elems - 1, .[.end]
given cache($n, {[*] .comb} ... *.chars == 1)
given cache($n, {[*] .comb} ... *.chars == 1)
Line 2,772: Line 2,772:
say "$d : ", .[^5]
say "$d : ", .[^5]
given (1..*).grep: *.&multiplicative-digital-root[1] == $d;
given (1..*).grep: *.&multiplicative-digital-root[1] == $d;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>123321: 3 8
<pre>123321: 3 8
Line 2,790: Line 2,790:


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


mdr: function [
mdr: function [
Line 2,828: Line 2,828:
]
]
prin newline
prin newline
]</lang>
]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,851: Line 2,851:
=={{header|REXX}}==
=={{header|REXX}}==
===idomatic version===
===idomatic version===
<lang rexx>/*REXX program finds the persistence and multiplicative digital root of some numbers.*/
<syntaxhighlight lang="rexx">/*REXX program finds the persistence and multiplicative digital root of some numbers.*/
numeric digits 100 /*increase the number of decimal digits*/
numeric digits 100 /*increase the number of decimal digits*/
parse arg x /*obtain optional arguments from the CL*/
parse arg x /*obtain optional arguments from the CL*/
Line 2,883: Line 2,883:
y=r
y=r
end /*p*/ /* [↑] wash, rinse, and repeat ··· */
end /*p*/ /* [↑] wash, rinse, and repeat ··· */
return p r /*return the persistence and the MDR. */</lang>
return p r /*return the persistence and the MDR. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 2,912: Line 2,912:
===ultra-fast version===
===ultra-fast version===
This fast version can handle a target of five hundred numbers with ease for the 2<sup>nd</sup> part of the task's requirement.
This fast version can handle a target of five hundred numbers with ease for the 2<sup>nd</sup> part of the task's requirement.
<lang rexx>/*REXX program finds the persistence and multiplicative digital root of some numbers.*/
<syntaxhighlight lang="rexx">/*REXX program finds the persistence and multiplicative digital root of some numbers.*/
numeric digits 2000 /*increase the number of decimal digits*/
numeric digits 2000 /*increase the number of decimal digits*/
parse arg target x /*obtain optional arguments from the CL*/
parse arg target x /*obtain optional arguments from the CL*/
Line 2,993: Line 2,993:
end /*p*/ /* [↑] wash, rinse, and repeat ··· */
end /*p*/ /* [↑] wash, rinse, and repeat ··· */
if s==1 then return r /*return multiplicative digital root. */
if s==1 then return r /*return multiplicative digital root. */
return p r /*return the persistence and the MDR. */</lang>
return p r /*return the persistence and the MDR. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 34 </tt>}}
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 34 </tt>}}
<pre>
<pre>
Line 3,021: Line 3,021:


===Similar===
===Similar===
<lang rexx>/*REXX pgm finds positive integers when shown in hex that can't be written with dec digs*/
<syntaxhighlight lang="rexx">/*REXX pgm finds positive integers when shown in hex that can't be written with dec digs*/
parse arg n cols . /*obtain optional argument from the CL.*/
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n = 100 /*Not specified? Then use the default.*/
if n=='' | n=="," then n = 100 /*Not specified? Then use the default.*/
Line 3,042: Line 3,042:


if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─') /*display the foot sep for output. */</lang>
say '───────┴'center("" , 1 + cols*(w+1), '─') /*display the foot sep for output. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 3,061: Line 3,061:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Digital root/Multiplicative digital root
# Project : Digital root/Multiplicative digital root


Line 3,122: Line 3,122:
end
end
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 3,145: Line 3,145:


===Similar===
===Similar===
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"
see "working..." + nl
see "working..." + nl
Line 3,167: Line 3,167:


see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,197: Line 3,197:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{works with|Ruby|2.4}}
{{works with|Ruby|2.4}}
<lang ruby>def mdroot(n)
<syntaxhighlight lang="ruby">def mdroot(n)
mdr, persist = n, 0
mdr, persist = n, 0
until mdr < 10 do
until mdr < 10 do
Line 3,215: Line 3,215:
end
end
puts "", "MDR: [n0..n4]", "=== ========"
puts "", "MDR: [n0..n4]", "=== ========"
10.times{|i| puts "%3d: %p" % [i, counter[i].first(5)]}</lang>
10.times{|i| puts "%3d: %p" % [i, counter[i].first(5)]}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,242: Line 3,242:
{{works with|Scala|2.9.x}}
{{works with|Scala|2.9.x}}


<lang Scala>import Stream._
<syntaxhighlight lang="scala">import Stream._


object MDR extends App {
object MDR extends App {
Line 3,268: Line 3,268:
.foreach{p => printf("%3s: [%s]\n",p._2,p._1.mkString(", "))}
.foreach{p => printf("%3s: [%s]\n",p._2,p._1.mkString(", "))}


}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,295: Line 3,295:
=={{header|Scheme}}==
=={{header|Scheme}}==
{{works with|Chez Scheme}}
{{works with|Chez Scheme}}
<lang scheme>; Convert an integer into a list of its digits.
<syntaxhighlight lang="scheme">; Convert an integer into a list of its digits.


(define integer->list
(define integer->list
Line 3,348: Line 3,348:
(printf "~5@a" mdr)
(printf "~5@a" mdr)
(for-each (lambda (int) (printf "~7@a" int)) (vector-ref mdrslsts mdr))
(for-each (lambda (int) (printf "~7@a" int)) (vector-ref mdrslsts mdr))
(newline)))</lang>
(newline)))</syntaxhighlight>
{{out}}
{{out}}
<pre> Integer Root Pers.
<pre> Integer Root Pers.
Line 3,384: Line 3,384:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang ruby>func mdroot(n) {
<syntaxhighlight lang="ruby">func mdroot(n) {
var (mdr, persist) = (n, 0)
var (mdr, persist) = (n, 0)
while (mdr >= 10) {
while (mdr >= 10) {
Line 3,405: Line 3,405:
 
 
say "\nMDR: [n0..n4]\n=== ========"
say "\nMDR: [n0..n4]\n=== ========"
10.times {|i| "%3d: %s\n".printf(i, counter{i}.first(5)) }</lang>
10.times {|i| "%3d: %s\n".printf(i, counter{i}.first(5)) }</syntaxhighlight>


{{out}}
{{out}}
Line 3,431: Line 3,431:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc mdr {n} {
<syntaxhighlight lang="tcl">proc mdr {n} {
if {$n < 0 || ![string is integer $n]} {
if {$n < 0 || ![string is integer $n]} {
error "must be an integer"
error "must be an integer"
Line 3,439: Line 3,439:
}
}
return [list $i $n]
return [list $i $n]
}</lang>
}</syntaxhighlight>
Demonstrating:
Demonstrating:
<lang tcl>puts "Number: MP MDR"
<syntaxhighlight lang="tcl">puts "Number: MP MDR"
puts [regsub -all . "Number: MP MDR" -]
puts [regsub -all . "Number: MP MDR" -]
foreach n {123321 7739 893 899998} {
foreach n {123321 7739 893 899998} {
Line 3,456: Line 3,456:
for {set i 0} {$i < 10} {incr i} {
for {set i 0} {$i < 10} {incr i} {
puts [format "%3d: (%s)" $i [join [lrange $accum($i) 0 4] ", "]]
puts [format "%3d: (%s)" $i [join [lrange $accum($i) 0 4] ", "]]
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,482: Line 3,482:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>// Only valid for n > 0 && base >= 2
<syntaxhighlight lang="vlang">// Only valid for n > 0 && base >= 2
fn mult(nn u64, base int) u64 {
fn mult(nn u64, base int) u64 {
mut n := nn
mut n := nn
Line 3,530: Line 3,530:
println("${i:3}: $l")
println("${i:3}: $l")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,561: Line 3,561:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
The size of some of the numbers here is such that we need to use BigInt.
The size of some of the numbers here is such that we need to use BigInt.
<lang ecmascript>import "/big" for BigInt
<syntaxhighlight lang="ecmascript">import "/big" for BigInt
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 3,621: Line 3,621:
Fmt.print("$3d: $s", i, l.toString)
Fmt.print("$3d: $s", i, l.toString)
i = i + 1
i = i + 1
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,649: Line 3,649:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|Python}}
{{trans|Python}}
<lang zkl>fcn mdroot(n){ // Multiplicative digital root
<syntaxhighlight lang="zkl">fcn mdroot(n){ // Multiplicative digital root
mdr := List(n);
mdr := List(n);
while (mdr[-1] > 9){
while (mdr[-1] > 9){
Line 3,655: Line 3,655:
}
}
return(mdr.len() - 1, mdr[-1]);
return(mdr.len() - 1, mdr[-1]);
}</lang>
}</syntaxhighlight>
<lang zkl>fcn mdroot(n){
<syntaxhighlight lang="zkl">fcn mdroot(n){
count:=0; mdr:=n;
count:=0; mdr:=n;
while(mdr > 9){
while(mdr > 9){
Line 3,669: Line 3,669:
}
}
return(count, mdr);
return(count, mdr);
}</lang>
}</syntaxhighlight>
<lang zkl>println("Number: (MP, MDR)\n======= =========");
<syntaxhighlight lang="zkl">println("Number: (MP, MDR)\n======= =========");
foreach n in (T(123321, 7739, 893, 899998))
foreach n in (T(123321, 7739, 893, 899998))
{ println("%7,d: %s".fmt(n, mdroot(n))) }
{ println("%7,d: %s".fmt(n, mdroot(n))) }
Line 3,684: Line 3,684:
foreach mp in (table.keys.sort()){
foreach mp in (table.keys.sort()){
println("%2d: %s".fmt(mp, table[mp][0,5])); //print first five values
println("%2d: %s".fmt(mp, table[mp][0,5])); //print first five values
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>