Minimum multiple of m where digital sum equals m: Difference between revisions

Add ABC
(Added Prolog solution)
(Add ABC)
 
(42 intermediate revisions by 21 users not shown)
Line 1:
{{draft task}}
 
Generate the sequence '''a(n)''' when each element is the minimum integer multiple '''m''' such that the digit sum of '''n''' times '''m''' is equal to '''n'''.
Line 21:
 
 
 
=={{header|11l}}==
{{trans|C++}}
 
<syntaxhighlight lang="11l">
F digit_sum(=n)
V sum = 0
L n > 0
sum += n % 10
n I/= 10
R sum
 
L(n) 1..70
L(m) 1..
I digit_sum(m * n) == n
print(‘#8’.format(m), end' I n % 10 == 0 {"\n"} E ‘ ’)
L.break
</syntaxhighlight>
 
{{out}}
<pre>
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # find the smallest m where mn = digit sum of n, n in 1 .. 70 #
# returns the digit sum of n, n must be >= 0 #
OP DIGITSUM = ( INT n )INT:
Line 47 ⟶ 76:
OD
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 59 ⟶ 88:
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN digit.sum n:
PUT 0 IN sum
WHILE n>0:
PUT sum + (n mod 10) IN sum
PUT floor (n/10) IN n
RETURN sum
 
HOW TO RETURN a131382 n:
PUT 1 IN m
WHILE n <> digit.sum (m*n): PUT m+1 IN m
RETURN m
 
FOR n IN {1..70}:
WRITE (a131382 n)>>9
IF n mod 10=0: WRITE /</syntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">{n←⍵ ⋄ (+∘1)⍣{n=+/10⊥⍣¯1⊢⍺×n}⊢0 } ¨ 7 10⍴⍳70</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 71 ⟶ 124:
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MINIMUM_MULTIPLE_OF_M_WHERE_DIGITAL_SUM_EQUALS_M.AWK
BEGIN {
start = 1
stop = 70
printf("A131382 %d-%d:\n",start,stop)
for (n=start; n<=stop; n++) {
for (m=1; ; m++) {
if (digit_sum(m*n,10) == n) {
printf("%9d%1s",m,++count%10?"":"\n")
break
}
}
}
exit(0)
}
function digit_sum(n,b, s) { # digital sum of n in base b
while (n) {
s += n % b
n = int(n/b)
}
return(s)
}
</syntaxhighlight>
{{out}}
<pre>
A131382 1-70:
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">c = 0
n = 1
while c < 70
Line 93 ⟶ 183:
n += 1
end while
end</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|BBC BASIC}}===
{{trans|C}}
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> FOR N%=1 TO 70
PRINT FNa131382(N%);
IF N% MOD 10 == 0 PRINT
NEXT
END
 
DEF FNa131382(n%) LOCAL m%
m%=1
WHILE n% <> FNdigit_sum(m% * n%)
m%+=1
ENDWHILE
=m%
 
DEF FNdigit_sum(n%) LOCAL sum%
WHILE n%
sum%+=n% MOD 10
n%/=10
ENDWHILE
=sum%</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 c = 0
120 n = 1
130 while c < 70
140 m = 1
150 while 1
160 nm = n*m
170 t = 0
180 while nm
190 t = t + nm mod 10
200 nm = floor(nm / 10)
210 wend
220 if t = n then exit while
230 m = m +1
240 wend
260 c = c +1
270 print using "########"; m;
280 if c mod 10 = 0 then print
290 n = n +1
300 wend
310 end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|FreeBASIC}}===
{{trans|Phix}}
<langsyntaxhighlight lang="freebasic">#define floor(x) ((x*2.0-0.5) Shr 1)
 
Dim As Integer c = 0, n = 1
Line 120 ⟶ 256:
n += 1
Loop
Sleep</syntaxhighlight>
</lang>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 130 ⟶ 265:
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim c As Integer = 0, n As Integer = 1
 
Do While c < 70
Dim m As Integer = 1
Do
Dim nm As Integer = n * m, t As Integer = 0
While nm
t += nm Mod 10
nm = Floor(nm / 10)
Wend
If t = n Then Break
m += 1
Loop
c += 1
Print Format(m, "######## ");
If c Mod 10 = 0 Then Print
n += 1
Loop
 
End</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="PureBasic">Procedure.i floor(n.d)
Result = (n*2.0-0.5)
ProcedureReturn Result >> 1
EndProcedure
 
OpenConsole()
 
c.i = 0
n.i = 1
While c < 70
m.i = 1
Repeat
nm.d = n*m
t.d = 0
While nm
t = t + Mod(nm, 10)
nm = floor(nm/10)
Wend
If t = n
Break
EndIf
m + 1
ForEver
c + 1
Print(RSet(Str(m), 8) + " ")
If Mod(c, 10) = 0
PrintN("")
EndIf
n + 1
Wend
 
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">LET c = 0
LET n = 1
 
DO WHILE c < 70
LET m = 1
DO
LET nm = n*m
LET t = 0
DO WHILE nm<>0
LET t = t+REMAINDER(nm,10)
LET nm = IP(nm/10)
LOOP
IF t = n THEN EXIT DO
LET m = m+1
LOOP
LET c = c+1
PRINT USING "######## ": m;
IF REMAINDER(c,10) = 0 THEN PRINT
LET n = n+1
LOOP
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "program name"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
c = 0
n = 1
DO WHILE c < 70
m = 1
DO WHILE 1
nm = n * m
t = 0
DO WHILE nm
t = t + nm MOD 10
nm = INT((nm / 10)+.5)
LOOP
IF t = n THEN EXIT DO
INC m
LOOP
INC c
PRINT FORMAT$("######## ", m);
IF c MOD 10 = 0 THEN PRINT
INC n
LOOP
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">c = 0
n = 1
while c < 70
Line 151 ⟶ 402:
n = n + 1
wend
end</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
unsigned digit_sum(unsigned n) {
Line 180 ⟶ 429:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 191 ⟶ 440:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 210 ⟶ 459:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 224 ⟶ 473:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">digit_sum = proc (n: int) returns (int)
sum: int := 0
while n > 0 do
Line 255 ⟶ 504:
if n // 10 = 0 then stream$putl(po, "") end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 266 ⟶ 515:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. A131382.
Line 307 ⟶ 556:
ADD-DIGIT.
ADD DIGITS(D) TO DIGITSUM.</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>A( 1) = 1
Line 351 ⟶ 600:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub digit_sum(n: uint32): (sum: uint8) is
Line 375 ⟶ 624:
end if;
n := n + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre>1 1 1 1 1 1 1 1 1 19
Line 386 ⟶ 635:
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">/* this is very slow even in emulation - if you're going to try it
* on a real 8-bit micro I'd recommend setting this back to 40;
* it does, however, eventually get there */
Line 419 ⟶ 668:
if (n & 7) = 0 then writeln() fi
od
corp</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1
Line 430 ⟶ 679:
103507 154981 150661 1333333 163918 322579 315873 937342
1076923 1030303 880597 1469116 1157971 12842857</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function SumDigits(N: integer): integer;
{Sum the integers in a number}
var T: integer;
begin
Result:=0;
repeat
begin
T:=N mod 10;
N:=N div 10;
Result:=Result+T;
end
until N<1;
end;
 
 
procedure MinimumMultipleM(Memo: TMemo);
{Find N's where DigitSum(N X M) = N.}
var N,M: integer;
var S: string;
begin
S:='';
for N:=1 to 70 do
for M:=1 to High(integer) do
begin
if SumDigits(M * N) = N then
begin
S:=S+Format('%8d',[M]);
if N mod 10 = 0 then S:=S+#$0D#$0A;
break;
end;
end;
Memo.Lines.Add(S);
end;
 
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 115797112842857
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc f n .
m = 1
repeat
h = m * n
sum = 0
while h > 0
sum += h mod 10
h = h div 10
.
until sum = n
m += 1
.
return m
.
for n = 1 to 70
write f n & " "
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Minimum multiple of m where digital sum equals m. Nigel Galloway: January 31st., 2022
let SoD n=let rec SoD n=function 0L->int n |g->SoD(n+g%10L)(g/10L) in SoD 0L n
let A131382(g:int)=let rec fN i=match SoD(i*int64(g)) with
n when n=g -> i
|n when n>g -> fN (i+1L)
|n -> fN (i+(int64(ceil(float(g-n)/float n))))
fN ((((pown 10L (g/9))-1L)+int64(g%9)*(pown 10L (g/9)))/int64 g)
Seq.initInfinite((+)1>>A131382)|>Seq.take 70|>Seq.chunkBySize 10|>Seq.iter(fun n->n|>Seq.iter(printf "%13d "); printfn "")
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|FOCAL}}==
<syntaxhighlight lang="focal">01.10 F N=1,40;D 2
01.20 Q
 
02.10 S M=0
02.20 S M=M+1
02.30 S D=N*M
02.40 D 3
02.50 I (N-S)2.2,2.6,2.2
02.60 T "N",%2,N," M",%6,M,!
 
03.10 S S=0
03.20 S E=FITR(D/10)
03.30 S S=S+(D-E*10)
03.40 S D=E
03.50 I (-D)3.2</syntaxhighlight>
{{out}}
<pre style='height:50ex;'>N= 1 M= 1
N= 2 M= 1
N= 3 M= 1
N= 4 M= 1
N= 5 M= 1
N= 6 M= 1
N= 7 M= 1
N= 8 M= 1
N= 9 M= 1
N= 10 M= 19
N= 11 M= 19
N= 12 M= 4
N= 13 M= 19
N= 14 M= 19
N= 15 M= 13
N= 16 M= 28
N= 17 M= 28
N= 18 M= 11
N= 19 M= 46
N= 20 M= 199
N= 21 M= 19
N= 22 M= 109
N= 23 M= 73
N= 24 M= 37
N= 25 M= 199
N= 26 M= 73
N= 27 M= 37
N= 28 M= 271
N= 29 M= 172
N= 30 M= 1333
N= 31 M= 289
N= 32 M= 559
N= 33 M= 1303
N= 34 M= 847
N= 35 M= 1657
N= 36 M= 833
N= 37 M= 1027
N= 38 M= 1576
N= 39 M= 1282
N= 40 M= 17497
</pre>
 
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: digit-sum ( u -- u )
dup 10 < if exit then
10 /mod recurse + ;
Line 447 ⟶ 851:
 
70 main
bye</langsyntaxhighlight>
 
{{out}}
Line 458 ⟶ 862:
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|Go}}==
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import "rcu"
 
func main() {
var res []int
for n := 1; n <= 70; n++ {
m := 1
for rcu.DigitSum(m*n, 10) != n {
m++
}
res = append(res, m)
}
rcu.PrintTable(res, 7, 10, true)
}</syntaxhighlight>
 
{{out}}
<pre>
1 1 1 1 1 1 1
1 1 19 19 4 19 19
13 28 28 11 46 199 19
109 73 37 199 73 37 271
172 1,333 289 559 1,303 847 1,657
833 1,027 1,576 1,282 17,497 4,339 2,119
2,323 10,909 11,111 12,826 14,617 14,581 16,102
199,999 17,449 38,269 56,413 37,037 1,108,909 142,498
103,507 154,981 150,661 1,333,333 163,918 322,579 315,873
937,342 1,076,923 1,030,303 880,597 1,469,116 1,157,971 12,842,857
</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.List (elemIndex, intercalate, transpose)
import Data.List.Split (chunksOf)
Line 492 ⟶ 928:
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</langsyntaxhighlight>
{{Out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 503 ⟶ 939:
Implementation:
 
<syntaxhighlight lang="j">
<lang J>
findfirst=: {{
($:@((+1+i.@+:)@#)@[`(+&{. I.)@.(1 e. ]) u) ,1
Line 510 ⟶ 946:
A131382=: {{y&{{x = sumdigits x*y}} findfirst}}"0
 
sumdigits=: +/@|:@(10&#.inv)</langsyntaxhighlight>
 
Task example:
<langsyntaxhighlight Jlang="j"> A131382 1+i.4 10
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497</langsyntaxhighlight>
 
Stretch example:
<langsyntaxhighlight Jlang="j"> A131382 41+i.3 10
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857</langsyntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang ="java">
public final class MinimumMultipleDigitSum {
 
public static void main(String[] aArgs) {
for ( int n = 1; n <= 70; n++ ) {
int k = 0;
while ( digitSum(k += n) != n );
System.out.print(String.format("%8d%s", k / n, ( n % 10 ) == 0 ? "\n" : " "));
}
}
private static int digitSum(int aN) {
int sum = 0;
while ( aN > 0 ) {
sum += aN % 10;
aN /= 10;
}
return sum;
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|Jakt}}==
Output format follows the C++ solution with a small difference in the leading whitespace.
 
<syntaxhighlight lang="jakt">
fn digital_sum(anon n: i64, accumulator: i64 = 0) -> i64 => match n {
0 => accumulator
else => digital_sum(n: n / 10, accumulator: accumulator + n % 10)
}
 
fn main() {
for n in 1..71 {
for m in (1..) {
if digital_sum(n * m) == n {
print("{: 9}", m)
if n % 10 == 0 {
println()
}
break
}
}
}
}
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with jq and gojq, the C and Go implementations of jq'''
 
Apart from a small tweak to the `lpad` function, the following program also works with jaq, the Rust
implementation of jq.
 
Assuming `digitSum` has been suitably defined to yield
the sum of the digits of a decimal number,
a jq expression for computing the $nth-term in the series is:
<syntaxhighlight lang=jq>
1 | until((. * $n) | digitSum == $n; . + 1)
</syntaxhighlight>
 
Let's abstract that into a self-contained jq function:
<syntaxhighlight lang=jq>
def minimum_integer_multiple:
def digitSum:
def add(s): reduce s as $_ (0; .+$_);
add(tostring | explode[] | . - 48);
 
. as $n
| 1 | until((. * $n) | digitSum == $n; . + 1);
</syntaxhighlight>
 
To display the first several values of the series in rows of 10 values,
we could first capture them as a single array, but to
avoid allocating unnecessary memory and to highlight jq's support for
stream-oriented processing,
we shall instead use the following function for grouping
the items in a (possibly non-finite) stream into arrays of
length at most $max:
<syntaxhighlight lang=jq>
# This function assumes that nan can be taken as the eos marker
def nwise(stream; $n):
foreach (stream, nan) as $x ([];
if length == $n then [$x] else . + [$x] end)
| if (.[-1] | isnan) and length>1 then .[:-1]
elif length == $n then .
else empty
end;
</syntaxhighlight>
 
The tasks can then be accomplished as follows:
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
nwise(
range(1; 71) | minimum_integer_multiple;
10)
| map(lpad(9)) | join(" ")
</syntaxhighlight>
{{output}}
<pre>
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">minproddigsum(n) = findfirst(i -> sum(digits(n * i)) == n, 1:typemax(Int32))
 
for j in 1:70
print(lpad(minproddigsum(j), 10), j % 7 == 0 ? "\n" : "")
end
</langsyntaxhighlight>{{out}}
<pre>
1 1 1 1 1 1 1
Line 546 ⟶ 1,113:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
VECTOR VALUES FMT = $2HA(,I2,4H) = ,I8*$
Line 563 ⟶ 1,130:
TRANSFER TO DIGIT
END OF FUNCTION
END OF PROGRAM</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>A( 1) = 1
Line 635 ⟶ 1,202:
A(69) = 1157971
A(70) = 12842857</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/strutils
 
func digitSum(n: Natural): int =
## Return the sum of digits of "n".
var n = n
while n != 0:
result.inc n mod 10
n = n div 10
 
iterator a131382(count: Natural): (int, int) =
## Yield the index and value of the first "count" elements
## of the sequence.
for n in 1..count:
var m = 1
while digitSum(m * n) != n:
inc m
yield (n, m)
 
for idx, n in a131382(70):
stdout.write align($n, 9)
if idx mod 10 == 0: stdout.write '\n'
</syntaxhighlight>
 
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
over strechted limit to 110.
Constructing minimal start number with sum of digits = m -> k+9+9+9+9+9+9 <BR>
Break up in parts of 4 digits.Could be more optimized.
Up to 100 at home.
<syntaxhighlight lang="pascal">program m_by_n_sumofdgts_m;
<lang pascal>
program m_by_n_sumofdgts_m;
//Like https://oeis.org/A131382/b131382.txt
{$IFDEF FPC} {$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$ENDIF}
uses
sysutils;
const
BASE = 10;
BASE4 = BASE*BASE*BASE*BASE;
MAXDGTSUM4 = 4*(BASE-1);
var
{$ALIGN 32}
SoD: array[0..BASE4-1] of byte;
{$ALIGN 32}
DtgBase4 :array[0..7] of Uint32;
DtgPartSums :array[0..7] of Uint32;
DgtSumBefore :array[0..7] of Uint32;
 
procedure Init_SoD;
Line 655 ⟶ 1,265:
begin
i := 0;
For d1 := 0 to BASE-1 do
For d0 := 0 to BASE-1 do
begin SoD[i]:= d1+d0;inc(i); end;
 
j := Base*Base;
For i := 1 to Base*Base-1 do
For d1 := 0 to BASE-1 do
For d0 := 0 to BASE-1 do
begin
SoD[j] := SoD[i]+d1+d0;
inc(j);
end;
end;
procedure OutDgt;
var
i : integer;
begin
for i := 5 downto 0 do
write(DtgBase4[i]:4);
writeln;
for i := 5 downto 0 do
write(DtgPartSums[i]:4);
writeln;
for i := 5 downto 0 do
write(DgtSumBefore[i]:4);
writeln;
end;
procedure InitDigitSums(m:NativeUint);
var
n,i,s: NativeUint;
begin
//constructing minimal number with sum of digits = m ;k+9+9+9+9+9+9
//21 -> 299
n := m;
if n>BASE then
begin
i := 1;
while n>BASE-1 do
begin
i *= BASE;
dec(n,BASE-1);
end;
n := i*(n+1)-1;
//make n multiple of m
n := (n div m)*m;
//m ending in 0
i := m;
while i mod BASE = 0 do
begin
n *= BASE;
i := i div BASE;
end;
end;
For i := 0 to 4 do
begin
s := n MOD BASE4;
DtgBase4[i] := s;
DtgPartSums[i] := SoD[s];
n := (n-s) DIV BASE4;
end;
s := 0;
For i := 3 downto 0 do
begin
s += DtgPartSums[i+1];
DgtSumBefore[i]:= s;
end;
end;
 
function SumOfDigits(n:nativeUint):NativeUint;
function CorrectSums(sum:NativeUint):NativeUint;
var
i,q,carry : NativeUintNativeInt;
begin
resulti := 0;
q := sum MOD Base4;
while n>0 do
sum := sum DIV Base4;
result := q;
DtgBase4[i] := q;
DtgPartSums[i] := SoD[q];
carry := 0;
repeat
inc(i);
q := sum MOD Base4+DtgBase4[i]+carry;
sum := sum DIV Base4;
carry := 0;
if q >= BASE4 then
begin
carry := 1;
q -= BASE4;
end;
DtgBase4[i]:= q;
DtgPartSums[i] := SoD[q];
until (sum =0) AND( carry = 0);
 
sum := 0;
For i := 3 downto 0 do
begin
qsum :+= n DIV BASE4DtgPartSums[i+1];
DgtSumBefore[i]:= sum;
result += SoD[n-BASE4*q];
n := qend;
end;
end;
 
function TakeJump(dgtSum,m:NativeUint):NativeUint;
var
n,i,j,carry : nativeInt;
begin
i := dgtsum div MAXDGTSUM4-1;
n := 0;
j := 1;
for i := i downto 0 do
Begin
n:= n*BASE4+DtgBase4[i];
j:= j*BASE4;
end;
n := ((j-n) DIV m)*m;
// writeln(n:10,DtgBase4[i]:10);
i := 0;
carry := 0;
repeat
j := DtgBase4[i]+ n mod BASE4 +carry;
n := n div BASE4;
carry := 0;
IF j >=BASE4 then
begin
j -= BASE4;
carry := 1;
end;
DtgBase4[i] := j;
DtgPartSums[i]:= SoD[j];
inc(i);
until (n= 0) AND (carry=0);
j := 0;
For i := 3 downto 0 do
begin
j += DtgPartSums[i+1];
DgtSumBefore[i]:= j;
end;
result := DtgBase4[0];
end;
 
procedure CalcN(m:NativeUint);
var
dgtsum,sum: NativeInt;
begin
InitDigitSums(m);
sum := DtgBase4[0];
dgtSum:= m-DgtSumBefore[0];
// while dgtsum+SoD[sum] <> m do
while dgtsum<>SoD[sum] do
begin
inc(sum,m);
if sum >= BASE4 then
begin
sum := CorrectSums(sum);
dgtSum:= m-DgtSumBefore[0];
if dgtSum > MAXDGTSUM4 then
begin
sum := TakeJump(dgtSum,m);
dgtSum:= m-DgtSumBefore[0];
end;
end;
end;
DtgBase4[0] := sum;
end;
var
T0:INt64;
i : NativeInt;
m,n: NativeUint;
Begin
T0 := GetTickCount64;
Init_SoD;
for m := 1 to 9070 do
begin
n := CalcN(m);
//constructing minimal number withCheck sum of digits = m ;k+9+9+9+9+9+9
n := SoD[DtgBase4[4]];
//21 -> 299
For i := 3 downto 0 do
if n>BASE then
n += SoD[DtgBase4[i]];
If n<>m then
begin
iwriteln('ERROR :=at 1',m);
while n>BASEHALT(-1 do);
end; begin
i *= BASE;
n := dec(n,BASE-1)DtgBase4[4];
For i end;:= 3 downto 0 do
n := i*(n*BASE4+1)-1DtgBase4[i];
write(n DIV m :15);
//make n multiple of m
n := (n div m)*m;
//m ending in 0
i := m;
while i mod BASE = 0 do
begin
n *= BASE;
i := i div BASE;
end;
end;
while SumOfDigits(n)<> m do
inc(n,m);
write(n DIV m:11);
if m mod 10 = 0 then
writeln;
end;
writeln;
end.
writeln('Total runtime ',GetTickCount64-T0,' ms');
</lang>
{$IFDEF WINDOWS} readln{$ENDIF}
end.</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Real time: 436.161660 s CPU share: 99.3548 %
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
4084507 5555554 6849163 37027027 13333333 11710513 11686987 11525641 12656962 374999986
12345679 60852439 72168553 82142857 117647047 93022093 103445977 227272726 112247191 1111111111
 
Total runtime 4 ms
@home: ..100
...100
4084507 5555554 6849163 37027027 13333333 11710513 11686987 11525641 12656962 374999986
12345679 60852439 72168553 82142857 117647047 93022093 103445977 227272726 112247191 1111111111
12345679 60852439 72168553 82142857 117647047 93022093 103445977 227272726 112247191 1111111111
658010989 652173913 731172043 849893617 2947368421 2083333228 1030927834 3969377551 11101010101 199999999999
658010989 652173913 731172043 849893617 2947368421 2083333228 1030927834 3969377551 11101010101 199999999999
 
Total runtime 642 ms
real 1m15,075 // 99 takes the longest time.
..110
</pre>
28900990099 5881372549 6796115533 18173076922 27619047619 18679245283 18691495327 36111111111 44862385321 1090909090909
 
Total runtime 36486 ms
@home
111 79009009009 17882 ms
112 80356249999 17906 ms
113 78761061946 17910 ms
114 87710526307 17916 ms
115 426086956513 18022 ms
116 258620688793 18033 ms
117 255547008547 18035 ms
118 414406779661 18039 ms
119 411756302521 18040 ms
120 4999999999999 26838 ms
 
121 2809909090909 27273 ms
122 811475409754 27290 ms
123 730081300813 27291 ms
124 2419193548387 27328 ms
125 5599999999999 29177 ms
126 2380873015873 29181 ms
127 3148818897637 29183 ms
128 5468749999921 29328 ms
129 5348836434031 29334 ms
130 46076923076923 32383 ms
 
131 6106793893129 32386 ms
132 28030303030303 32559 ms
133 6766917293233 32560 ms
134 22388058880597 32577 ms
135 37037037037037 32665 ms
136 44044117647058 32712 ms
137 56919700729927 32774 ms
138 36231884057971 32775 ms
139 49568345323741 32775 ms
140 571427857142857 58364 ms
 
141 63822694964539 58366 ms
142 140140838028169 58378 ms
143 391606993006993 58606 ms
144 277777777777777 58698 ms
145 482751724137931 58929 ms
146 471917801369863 58959 ms
147 401360544217687 58961 ms
148 1081081081081081 59207 ms
149 536912751677851 59213 ms
150 13333333333333333 728689 ms</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Minimum_multiple_of_m_where_digital_sum_equals_m
Line 753 ⟶ 1,548:
$m;
} 1 .. 70;
print "@answers\n\n" =~ s/.{65}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 766 ⟶ 1,561:
=={{header|Phix}}==
{{trans|XPL0}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
Line 784 ⟶ 1,579:
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 794 ⟶ 1,589:
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
===much faster===
<small>(Not that the above is particularly slow, but you certainly wouldn't want to push it much past 70)</small>
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/mmnn.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\mmnn.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- (for the final divide only)</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">rackup5</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">rackup11</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">r5</span><span style="color: #0000FF;">=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">nextup11</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mmnn</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: #000080;font-style:italic;">--
-- minimum multiple of n that sums to n
--
-- Just as we can emulate normal counting using an array of digits,
-- we can count while maintaining a sum of digits, which means we
-- have to find both a decrementable and an incrementable digit,
-- moving the former to the end(ish), or if no "" prepend a 1, eg
-- 5 -&gt; 14 -&gt; 23 -&gt; 32 -&gt; 41 -&gt; 50 -&gt; 104 -&gt; 113 -&gt; 122 -&gt; 131 -&gt;
-- 140 -&gt; 203 .. 230 -&gt; 302 .. 500 -&gt; 1004 .. 10004, etc.
-- Another example, this time summing to 14 (the above was to 5!):
-- 59 -&gt; 68 .. 95 -&gt; 149 .. 194 -&gt; 239 .. 293 -&gt; 329 .. 392 -&gt;
-- 419 .. 491 -&gt; 509 .. 590 -&gt; 608 .. 680 -&gt; 707 .. 770 -&gt; 806
-- .. 860 -&gt; 905 .. 950 -&gt; 1049, etc.
-- in that last case, what we actually need to do is decrement the
-- 5 (-&gt;940), then reverse the whole thing, and then prepend a 1,
-- likewise if we increment a middle digit, dec then reverse(rest).
-- In the extreme case you would want 1 -&gt; 10 -&gt; 100 -&gt; 1000, etc,
-- although since 1 passes muster we would never actually do that.
--</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"0"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- avoid remainder(x,0), and tz=inf</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- this many trailing 9s...</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">9</span><span style="color: #0000FF;">*</span><span style="color: #000000;">l</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- ...and this first digit</span>
<span style="color: #000000;">n0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'9'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- eg n=14 -&gt; "59"</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000080;font-style:italic;">-- optimising for trailing zeroes makes a truly *huge* difference</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">n0</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">10</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'0'</span> <span style="color: #000080;font-style:italic;">-- (these be static, aka forever past "l")</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">r5</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- ten-fold improvement on some</span>
<span style="color: #000000;">r11</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- took 165 from 1min 25s to 6s,
-- (even faster now r5 working)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">r5</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rackup5</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">r11</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rackup11</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r5</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- calculate remainder(digits,n) digit-wise; faster than mpz</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rem</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (nb not "l")</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span>
<span style="color: #000000;">rem</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rem</span><span style="color: #0000FF;">+</span><span style="color: #000000;">rn</span><span style="color: #0000FF;">*</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">rn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rn</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rem</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000080;font-style:italic;">-- (only triggers on eg 202, 222, 271 and higher)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #0000FF;">?{</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">r11</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- decrementable digit position</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]></span><span style="color: #008000;">'0'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">d</span> <span style="color: #008080;">and</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]<</span><span style="color: #008000;">'9'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- (so digits[1]=9 or no decrementable)</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">else</span>
<span style="color: #000080;font-style:italic;">-- Same as above but with additional rules</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nextup11</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_fdiv_q_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">rackup5</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- converts digits to next (odd) number divisible by 5, while
-- maintaining the exact same sum of digits unaltered.
-- Techincally divisible by 5 is ends in 5 or 0, but since we
-- have already dealt with 10s, we know that n0 (above) must
-- be odd and hence digits[1..l] always end in 5, so we play
-- a neat and simple little trick of cropping the length by 1
-- to make the final 5 static, same as we do with trailing 0s.
--</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">'5'</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'9'</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- eg 299 -&gt; 295 ie -4</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'5'</span> <span style="color: #000080;font-style:italic;">-- or 799 -&gt; 795 ""</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'5'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">4</span> <span style="color: #000080;font-style:italic;">-- eg 295 -&gt; 695 == +4</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'9'</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">-</span><span style="color: #000000;">5</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">digits</span> <span style="color: #000080;font-style:italic;">-- eg 795 -&gt; 2995 ie +2+2</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</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;">swarpp11</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- special pairwise swapping/reversal for divisible by 11 numbers, eg:
--
-- | 7654321 | , | 653421 | , | 54321 | , | 4321 | , | 321 | , | 21 | , | 1 |
-- &gt; 1234567 &lt; &gt; 213465 &lt; &gt; 12345 &lt; &gt; 2143 &lt; &gt; 123 &lt; &gt; 21 &lt; &gt; 1 &lt;
--
-- (ie after bumping some prior digits following the rules (see below), we
-- need to make the remaining digits (7 or less in the above) as small as
-- legally possible but obviously without breaking any of the said rules.)
--</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">si</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">si</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">nextup11</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- Divisible by 11 uses the additional rule that the
-- sum of odd place digits - sum of even place digits
-- must be a multiple of 11 (including zero).
-- Hence we look for an odd/even decrementable digit
-- that can pair with an odd/even incrementable digit,
-- or an odd/even decrementable-by-11 set to pair off
-- with an even/odd incrementable-by-11 set/sum.
-- (for the latter see the bTransferable11 flag)
-- Likewise we use a slightly different method to
-- increase the number of digits, and kick-start
-- things off with the rackup11() routine below.
--
-- Aside: A variation is probably extendible to 101,
-- expressed using alternating pairs of digits, eg:
-- 74*11 = 814, 8-1+4 = 11
-- 7242*101 = 731442, 73-14+42 = 101
-- (which really is the same rule writ different)
-- There is also sum of pairs/triples for 11 and 111:
-- 74*11 = 814, 8+14 = 22
-- 237*111 = 26307, 26+307 = 333
-- (not entirely sure that can actually be applied)
--</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- a digit</span>
<span style="color: #000000;">dd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- decrementable digit position (scratch)</span>
<span style="color: #000000;">ddd</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- decrementable digit value """</span>
<span style="color: #000000;">td</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- total decrementable digits</span>
<span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- total incrementable digits</span>
<span style="color: #000000;">od</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- decrementable odd digit position</span>
<span style="color: #000000;">ed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- decrementable even digit position</span>
<span style="color: #000000;">dod</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- decrementable odd digit value</span>
<span style="color: #000000;">ded</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- decrementable even digit value</span>
<span style="color: #000000;">tod</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- total odd decrementable digits</span>
<span style="color: #000000;">ted</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- total even decrementable digits</span>
<span style="color: #000000;">iod</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- total odd incrementable digits</span>
<span style="color: #000000;">ied</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- total even incrementable digits</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">bIncAbleFound</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">bTransferable11</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">wasdig</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">transdigits</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">tod</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">d</span>
<span style="color: #000000;">iod</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">-</span><span style="color: #000000;">d</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">od</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">od</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
<span style="color: #000000;">dod</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">dd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">od</span>
<span style="color: #000000;">ddd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dod</span>
<span style="color: #000000;">bIncAbleFound</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;"><</span><span style="color: #000000;">9</span> <span style="color: #008080;">and</span> <span style="color: #000000;">dd</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">bTransferable11</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">iod</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">11</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ted</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">11</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">ted</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">d</span>
<span style="color: #000000;">ied</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">-</span><span style="color: #000000;">d</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ed</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">ed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
<span style="color: #000000;">ded</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">dd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ed</span>
<span style="color: #000000;">ddd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ded</span>
<span style="color: #000000;">bIncAbleFound</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;"><</span><span style="color: #000000;">9</span> <span style="color: #008080;">and</span> <span style="color: #000000;">dd</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">bTransferable11</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ied</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">11</span> <span style="color: #008080;">and</span> <span style="color: #000000;">tod</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">11</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bTransferable11</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">od</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ed</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)=</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)?{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}:{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">td</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">}</span>
<span style="color: #000000;">transdigits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">t</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">ti</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">od</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'9'</span><span style="color: #0000FF;">-</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">od</span><span style="color: #0000FF;">],</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">ti</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">t</span>
<span style="color: #000000;">transdigits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">od</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">t</span>
<span style="color: #000000;">od</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">td</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ed</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ed</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">td</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">td</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">t</span>
<span style="color: #000000;">transdigits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ed</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">t</span>
<span style="color: #000000;">ed</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">transdigits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">swarpp11</span><span style="color: #0000FF;">(</span><span style="color: #000000;">transdigits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">transdigits</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">digits</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">bTransferable11</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">elsif</span> <span style="color: #008080;">not</span> <span style="color: #000000;">bIncAbleFound</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">transdigits</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bIncAbleFound</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dd</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ddd</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'1'</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">swarpp11</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bTransferable11</span> <span style="color: #008080;">and</span> <span style="color: #000000;">transdigits</span><span style="color: #0000FF;"><</span><span style="color: #000000;">digits</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">transdigits</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- (so digits[1]=9 or no o/e decrementable)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">dd</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ddd</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)?{</span><span style="color: #000000;">ed</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ded</span><span style="color: #0000FF;">}:{</span><span style="color: #000000;">od</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dod</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dd</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ddd</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</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;">rackup11</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">r5</span><span style="color: #0000FF;">=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- Converts digits to next number divisible by 11, while
-- maintaining the exact same sum of digits unaltered, eg
-- 29 -&gt; 209, 499 -&gt; 2299, 6999 -&gt; 42999, 89999 -&gt; 449999,
-- 1999999 -&gt; 6499999, 39999999 -&gt; 66999999, 599999999 -&gt;
-- 869999999, 7999999999 -&gt; 8899999999, 99999999999 -&gt;
-- 1098999999999, 2999999999999 -&gt; 11999999999999.
--
-- For divisible by 55 we've already got the first divisible by 5 and
-- set that in stone, aka digits[l+1] is 5, so here we must take that
-- quickly into account.
--</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">r5</span> <span style="color: #008080;">then</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">od</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">11</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">ed</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">11</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">od</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ed</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">od</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ed</span><span style="color: #0000FF;">=</span><span style="color: #000000;">11</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">r5</span> <span style="color: #008080;">then</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">od</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">ed</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">--
-- Aside: I couldn't quite get my head around the apparently convoluted rules
-- of which way to go, but obviously if od=2 and ed=9 then either:
-- add 9 to the odd digits and subtract it from the even digits, or
-- add 2 to the even digits and subtract it from the odd digits,
-- and then in both cases add prefixes for anything you couldn't do.
-- The simplest thing is to just do both and select the smaller,
-- plus a couple of quick checks (wood|weod&gt;9) for odd-od busted.
--</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- a digit</span>
<span style="color: #000000;">woed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ed</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">weed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ed</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- work vars for odd+ed, even-ed</span>
<span style="color: #000000;">wood</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">od</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">weod</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">od</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- work vars for odd-od, even+od</span>
<span style="color: #000000;">loed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">l</span> <span style="color: #000080;font-style:italic;">-- new value for l should we switch</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">odd_ed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span> <span style="color: #000080;font-style:italic;">-- (odd-od done in digits direct)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'9'</span><span style="color: #0000FF;">-</span><span style="color: #000000;">odd_ed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">woed</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- add ed to odd digits</span>
<span style="color: #000000;">odd_ed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">d</span>
<span style="color: #000000;">woed</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">d</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">wood</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- subtract od from ""</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">d</span>
<span style="color: #000000;">wood</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">d</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">odd_ed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">weed</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- subtract ed from even</span>
<span style="color: #000000;">odd_ed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">d</span>
<span style="color: #000000;">weed</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">d</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'9'</span><span style="color: #0000FF;">-</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">weod</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- add od to even digits</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">d</span>
<span style="color: #000000;">weod</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">d</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">woed</span> <span style="color: #008080;">or</span> <span style="color: #000000;">weed</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">odd_ed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">weed</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">odd_ed</span>
<span style="color: #000000;">loed</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">woed</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">odd_ed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">woed</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">odd_ed</span>
<span style="color: #000000;">loed</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">wood</span> <span style="color: #008080;">or</span> <span style="color: #000000;">weod</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">weod</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">digits</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">wood</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">wood</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">digits</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">loed</span><span style="color: #0000FF;"><</span><span style="color: #000000;">l</span>
<span style="color: #008080;">or</span> <span style="color: #000000;">weod</span><span style="color: #0000FF;">></span><span style="color: #000000;">9</span>
<span style="color: #008080;">or</span> <span style="color: #000000;">wood</span><span style="color: #0000FF;">></span><span style="color: #000000;">9</span>
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">loed</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">and</span> <span style="color: #000000;">odd_ed</span><span style="color: #0000FF;"><</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">odd_ed</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">loed</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">200</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- 0.7s
--for n=1 to 274 do -- 12.8s, but mmnn(275) takes 2 minutes and 25s
--for n=1 to 369 do -- 2 minutes 31s, but I gave up on 370
--for n=1 to 666 do -- 3 minutes 31s, skipping 11 tricky ones
--if not find(n,{275,370,(404),481,505,518,542,625,629,656,666}) then</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">100</span><span style="color: #0000FF;">?</span><span style="color: #000000;">10</span><span style="color: #0000FF;">:</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</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;">"\n%3d:"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</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;">" %-11s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mmnn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
<span style="color: #000080;font-style:italic;">--end if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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;">"\n1,000,000: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mmnn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e6</span><span style="color: #0000FF;">))})</span> <span style="color: #000080;font-style:italic;">-- (0.0s)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
1: 1 1 1 1 1 1 1 1 1 19
11: 19 4 19 19 13 28 28 11 46 199
21: 19 109 73 37 199 73 37 271 172 1333
31: 289 559 1303 847 1657 833 1027 1576 1282 17497
41: 4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
51: 17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
61: 163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
71: 4084507 5555554 6849163 37027027 13333333 11710513 11686987 11525641 12656962 374999986
81: 12345679 60852439 72168553 82142857 117647047 93022093 103445977 227272726 112247191 1111111111
91: 658010989 652173913 731172043 849893617 2947368421 2083333228 1030927834 3969377551 11101010101 199999999999
101: 28900990099 5881372549 6796115533 18173076922 27619047619
106: 18679245283 18691495327 36111111111 44862385321 1090909090909
111: 79009009009 80356249999 78761061946 87710526307 426086956513
116: 258620688793 255547008547 414406779661 411756302521 4999999999999
121: 2809909090909 811475409754 730081300813 2419193548387 5599999999999
126: 2380873015873 3148818897637 5468749999921 5348836434031 46076923076923
131: 6106793893129 28030303030303 6766917293233 22388058880597 37037037037037
136: 44044117647058 56919700729927 36231884057971 49568345323741 571427857142857
141: 63822694964539 140140838028169 391606993006993 277777777777777 482751724137931
146: 471917801369863 401360544217687 1081081081081081 536912751677851 13333333333333333
151: 1258278145629139 3217105263157894 1307189477124183 3830512987012987 5161290322516129
156: 4423076923076923 3821656050955414 6202531582278481 5660371069181761 124999999999999993
161: 12415527950310559 12345679012345679 18404907975398773 48773170731707317 430303030303030303
166: 48186144578313253 53233532874251497 119041666666666666 59165680473372781 588235235294117647
171: 169590643216374269 290691860465116279 289017341039884393 344827586206895977 1142857142857142857
176: 511363636363636363 507903954802259887 1123594938202247191 1061452513966424581 11111111111111111111
181: 2154143646408839779 3845989010989010989 3278142075956284153 5380434239130434782 37027027027027027027
186: 9677419354838172043 5347053475935828877 20744680845744680851 20105291005291005291 157894731578947368421
191: 15706806282722513089 41666666666666666614 36269424870466321243 46391752577319535567 153845641025641025641
196: 147959183673469382653 101517766497461928934 101010101010101010101 201005025125628040201 19999999999999999999999
1,000,000: 19999999999999999999...99999999999999999999 (111,112 digits)
"0.7s"
</pre>
As shown commented out, it will carry on a fair bit further, but I gave up on <del>275</del> 370, and <del>it looks to me like some quite wierd patterns are starting to appear</del> now I'm pretty sure any percieved pretty patterns were figments of my fetid imagination, far too random/sparse to be of any significant practical use.
<pre style="font-size: 9px">
201: 293532338308407960199 1900990099009900990099 438374384235960591133 490195588235294117647 1902439024390243902439
206: 1407766990291213592233 966183574879227052657 2836538461538461538461 10526315784688995215311 23809047619047619047619
211: 2843601895260663507109 4716933490566037735849 4225305164318779342723 9345794387803738317757 27441855813953488372093
216: 18518518518518518518518 18433179262672811059447 27518348623848623853211 27397210045662100456621 1090909090909090909090909
221: 36199049773710407239819 259009009009009009009009 40358744394618834080713 223169642857142857142812 311111111111111111111111
226: 216371681415929203539823 220264317180615859030837 307017543859649122368421 301310043668117903930131 3043434782608695652173913
231: 1904329004329004329004329 1293103448275861637931034 429184549351931330472103 1282047008547008547008547 2978723404255319148893617
236: 2923728813135593220338983 2531223628270042194092827 3361302521008403361302521 3682004184100418410041841 41666666666666666666666662
241: 4149377593360995846473029 19421487603305785123553719 4115226337448559670781893 20491803278688483606557377 32648979591836734693877551
246: 35731300813008130081300813 32388258704048582995951417 80604838306451612903225806 35742971887550200803212851 1999999999999999999999999999
251: 79641434262549800796812749 119047619047619047619047619 260830039525691699604743083 236220472047244094488188937 352941176470196078431372549
256: 390624999999999999999960898 311284046688715953307392607 348837209301937984496124031 1081003861003861003861003861 11534576923076923076923076923
261: 766283524904210727969348659 1908014885496183206106870229 1520912547528517110266121673 2992424242424242424242424242 7546792452452830188679245283
266: 3007481203007518796992481203 3333333295880149812734082397 10820895485074626865671641791 7397769516728620817843866171 37037037037037037037037037037
271: 31690036900369003690036900369 25735294117647055147058823529 29263003663003663003663003663 29197007299270072992700729927 1126909090909090909090909090909
276: 36231880434782608695652173913 36101083032490974729238266787 107910071942446039568345323741 71684229390681003584229390681 1428571428571428571428571428571
281: 209964377188612099644128113879 212765957446808475177304964539 247314487632508833886925795053 352112676056337676056338028169 1052631578947017543859649122807
286: 1006993006993006993006993006993 1003484320557491289198606271777 1736111111111111111111111111076 1384083041522145328719723183391 13793103448275862068965517237931
291: 2061512027491374570446735395189 3356164041095890410958904109589 2730375426621160409215017064843 3061224149659863945578231292517 13559322033898305084745762711861
296: 16858108108108108108108108108108 37003367003367003367003367003367 16744966442919463087248322147651 16655518394648829431438127090301 1333333333333333333333333333333333
301: 23255813953488372092691029900299 29801324503311258278142384102649 130330033003300330033003300330033 98683881578947368421052631578947 196393442622950819672127868852459
306: 98039215686241830065359477124183 130293159283061889250814332247557 487012987012987012987012987012987 194174433656925566343042071197411 1935483870964516129032258064516129
311: 256913183279742765273311897106109 929483974358974358974358974358974 316293929712460063865814696485623 923248407643312101910828025477707 1873015873015873015873015873015873
316: 1895569303797468354430379746835443 1577287066246056782331198738170347 2201226100628930817610062893081761 10655172413793103448275862065830721 31249999999999999999999999999999684
321: 2800623052959498442367601246105919 6211177018633540372670807450310559 6188854489164086687306501547987613 12037037037037037037037037037037037 27692276923076923076923076923076923
326: 15337392638036809815950920245398773 18318012232415902140672782874617737 30457317073170731707317073170731707 24316109422492370820668693009118541 1030303030303030303030303030303030303
331: 57401812688821751963743202416918429 90331325301204819277108433734939759 203003003003003003003003003003003003 146706586826347305389218562874251497 238805970149253728358208955223880597
336: 208333333333333333333333333333333333 204747774480712166172106824925815727 266272189348816568044378698224852071 265191740412979351032448377581120941 2941176470587941176470588235294117647
341: 1612903225806451319648093841642228739 877192953216374269005847953216374269 1165889212827988338192419533527696793 2296511337209302325581395348837209302 2608695652173910144927536231884057971
346: 2283208092485549132947976878612716763 2305187319884726224783861671469740634 2873563218390804597701146551724137931 2865329512893982808020057306590257851 142856857142857142857142857142857142857
351: 11392849002849002849002849002849002849 19602272727272727272727272727272727244 14164300283286118980169971671388101983 16949152542372881355932203389802259887 28169014084504225352112676056338028169
356: 28061797752808988764044943820224719073 22408963585434173389355742296918767507 55586592178770949720670388268156424581 55431754874649025069637883008356545961 833333333333333333333333333333333333333
361: 135734072022160637119113296398891966759 165690607734806629834254143646408839779 212093663911845730027548209366391184573 217032967032967032967032967032967032967 1095586301369863013698630136986301369863
366: 270491803278688524590163934425956284153 272479564032425067847411444141689373297 1086956521739130163043478260869565217391 1027100271002710027100271002710027100271
"2 minutes and 29s"
</pre>
===much simpler, to 1000===
Uses dynamic programming, at least I think it does.
Maintains a grid of digit sum against remainder in the form of the (final) digit that got us there first (so it's the smallest) along with a (parent) link to the chain of preceding digits. In effect, we are simply performing a breadth first search of unvisited nodes to get from {0,0} aka "" to {n,0} ie a digital sum of n with no remainder. The clever (ok, obvious if you prefer) maths bit is remainder(<parent's remainder>*10,n) when appending each individual digit.
 
While a fair bit slower on most individual numbers (esp under pwa/p2js) it hits far fewer bottlenecks such as that 275 above, and at least past 200 turns out to be quite a bit faster. However there is no equivalent trailing zero optimisation that I can see, and it caps out at 1e4 on 32 bit, 2e4 on 64 bit, for obvious reasons the square root of the hard limits of the above (assuming a number the code above doesn't pick a fight with).<br>
Translation of the C++ code on [[oeis:A002998|A002998]] but with the additional final divide for [[oeis:A131382|A131382]] and significantly clearer imnsho.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- (for the final divide only)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mmnn</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;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"0"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (edge case)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">*(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)),</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0"</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">parent</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">*(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)),</span> <span style="color: #000000;">queue</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dsum</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">residue</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (found or queue not empty at end of loop)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">dsum</span><span style="color: #0000FF;">></span><span style="color: #000000;">n</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">dsum</span><span style="color: #0000FF;">=</span><span style="color: #000000;">n</span> <span style="color: #008080;">and</span> <span style="color: #000000;">residue</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- success!!</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'0'</span><span style="color: #0000FF;">+</span><span style="color: #000000;">digit</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">pdx</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pdx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">res</span>
<span style="color: #000000;">pdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">parent</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pdx</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_fdiv_q_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">drx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dsum</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">residue</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">drx</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">' '</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">drx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'0'</span><span style="color: #0000FF;">+</span><span style="color: #000000;">digit</span>
<span style="color: #000000;">parent</span><span style="color: #0000FF;">[</span><span style="color: #000000;">drx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pdx</span>
<span style="color: #000000;">queue</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queue</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">dsum</span><span style="color: #0000FF;">,</span><span style="color: #000000;">residue</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">dsum</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">residue</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">residue</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queue</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"FAIL"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">dsum</span><span style="color: #0000FF;">,</span><span style="color: #000000;">residue</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">queue</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">queue</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">queue</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
<span style="color: #000000;">pdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dsum</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">residue</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #000000;">residue</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">residue</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #008000;">"what???"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">llen</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">200</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- 1.2s (15.4s under pwa/p2js)
--for n=1 to 274 do -- 3.1s
--for n=1 to 369 do -- 7.1s
--for n=1 to 500 do -- 19s
--for n=1 to 1000 do -- 2 mins 29s</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mmnn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">llen</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">bCR</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">101</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">llen</span><span style="color: #0000FF;">></span><span style="color: #000000;">118</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bCR</span> <span style="color: #008080;">then</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;">"\n%3d:"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #000000;">llen</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</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;">" %-11s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<span style="color: #000080;font-style:italic;">-- printf(1,"%d %s\n",{n,mmnn(n)}) // Generate b-file for A131382 (1..1000)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">--printf(1,"%d %s\n",{1e3,mmnn(1e3)}) -- 0.6s
--printf(1,"%d %s\n",{1e4,mmnn(1e4)}) -- 1min 8s
--printf(1,"%d %s\n",{2e4,mmnn(2e4)}) -- crashes on 32 bit, 6 minutes and 18s on 64 bit
--printf(1,"%d %s\n",{1e6,mmnn(1e6)}) -- not a chance!</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;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
Coincidentally taking exactly as long to get to 1000 as the above took to get to 369, as above with slightly different line breaks, plus
<pre>
<snip>
995: 2010040201005025125628140703517587939698492462311557788944723618090452261306532663316582914572864321608040201
996: 1907630522088353413654618473895481927710843373493975903614457831325301204819277108433734939759036144578313253
997: 1003009027081243731193580742226579739217652958876629889669007021063189568706118355065195586760279839518555667
998: 2004008016032064128256513026052104208416833667234468937875751503006012024048096192384769539078156312625250501
999: 1001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001
1000: 1999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
"2 minutes and 29s"
</pre>
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
{{Trans|ALGOL 68|but finds the multiples of 1..39, as the 8080 PL/M compiler supports (unsigned) 8 and 16 bit numbers. The minimum multiple of 40 whose digit sum is 40 is larger than 65535.}}
<syntaxhighlight lang="plm">
100H: /* FIND THE SMALLEST M WHERE M*N = DIGIT SUM OF N, N IN 1 .. 70 */
 
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
 
/* TASK */
 
DECLARE TRUE LITERALLY '0FFH', FALSE LITERALLY '0';
DIGIT$SUM: PROCEDURE( N )ADDRESS; /* RETURNS THE DIGIT SUM OF N */
DECLARE N ADDRESS;
IF N < 10 THEN RETURN N;
ELSE DO;
DECLARE ( RESULT, V ) ADDRESS;
RESULT = N MOD 10;
V = N / 10;
DO WHILE V > 0;
RESULT = RESULT + ( V MOD 10 );
V = V / 10;
END;
RETURN RESULT;
END;
END DIGIT$SUM ;
 
/* SHOW THE MINIMUM MULTIPLE OF N WHERE THE DIGIT SUM OF THE MULTIPLE IS N */
DECLARE ( M, N ) ADDRESS;
DECLARE FOUND$MULTIPLE BYTE;
DO N = 1 TO 39;
FOUND$MULTIPLE = FALSE;
M = 0;
DO WHILE NOT FOUND$MULTIPLE;
M = M + 1;
IF DIGIT$SUM( M * N ) = N THEN DO;
FOUND$MULTIPLE = TRUE;
CALL PR$CHAR( ' ' );
IF M < 10000 THEN DO;
CALL PR$CHAR( ' ' );
IF M < 1000 THEN DO;
CALL PR$CHAR( ' ' );
IF M < 100 THEN DO;
CALL PR$CHAR( ' ' );
IF M < 10 THEN CALL PR$CHAR( ' ' );
END;
END;
END;
CALL PR$NUMBER( M );
IF N MOD 8 = 0 THEN CALL PR$NL;
END;
END;
END;
 
EOF
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1 1 1
1 19 19 4 19 19 13 28
28 11 46 199 19 109 73 37
199 73 37 271 172 1333 289 559
1303 847 1657 833 1027 1576 1282
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">main:-
between(1, 40, N),
min_mult_dsum(N, M),
Line 827 ⟶ 2,216:
divmod(N, 10, M, Digit),
S2 is S1 + Digit,
digit_sum(M, Sum, S2).</langsyntaxhighlight>
 
{{out}}
Line 838 ⟶ 2,227:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''A131382'''
 
from itertools import count, islice
Line 928 ⟶ 2,317:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 937 ⟶ 2,326:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>sub min-mult-dsum ($n) { (1..∞).first: (* × $n).comb.sum == $n }
 
say .fmt("%2d: ") ~ .&min-mult-dsum for flat 1..40, 41..70;</langsyntaxhighlight>
{{out}}
<pre> 1: 1
Line 1,011 ⟶ 2,400:
69: 1157971
70: 12842857</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Table MinMult 1 70 10 10>;
};
 
MinMult {
s.N = <MinMult s.N 1>;
s.N s.M, <DigSum <* s.M s.N>>: s.N = s.M;
s.N s.M = <MinMult s.N <+ s.M 1>>;
};
 
DigSum {
0 = 0;
s.N, <Symb s.N>: s.D e.R = <+ <Numb s.D> <DigSum <Numb e.R>>>;
};
 
Cell {
s.Size e.X, <Lenw e.X>: s.Cur e.Y,
<Compare s.Cur s.Size>: '-' = <Cell s.Size ' 'e.X>;
s.Size e.X = e.X;
};
 
Table {
s.F s.N s.Max s.Width s.CW =
<Table s.F s.N s.Max s.Width s.CW s.Width ()>;
s.F s.N s.Max s.Width s.CW 0 (e.Line) =
<Prout e.Line>
<Table s.F s.N s.Max s.Width s.CW s.Width ()>;
s.F s.N s.Max s.Width s.CW s.Col (e.Line), <Compare s.N s.Max>: '+' =
<Prout e.Line>;
s.F s.N s.Max s.Width s.CW s.Col (e.Line) =
<Table s.F <+ s.N 1> s.Max s.Width s.CW <- s.Col 1>
(e.Line <Cell s.CW <Symb <Mu s.F s.N>>>)>;
};</syntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857</pre>
 
=={{header|RPL}}==
≪ 0 SWAP '''WHILE''' DUP '''REPEAT'''
10 MOD LAST / FLOOR SWAP ROT + SWAP
'''END''' DROP
≫ ‘'''∑DIG'''’ STO
≪ 0 '''DO''' 1 + DUP2 * '''UNTIL ∑DIG''' 3 PICK == '''END''' SWAP DROP
≫ ‘'''A131382'''’ STO
 
≪ { } 1 40 '''FOR''' j j '''A131382''' + '''NEXT''' ≫ EVAL
{{out}}
<pre>
1: { 1 1 1 1 1 1 1 1 1 19 19 4 19 19 13 28 28 11 46 199 19 109 73 37 199 73 37 271 172 1333 289 559 1303 847 1657 833 1027 1576 1282 17497 }
</pre>
Runs in 98 minutes on a HP-28S.
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">a131382 = (0..).lazy.map{|n| (1..).detect{|m|(n*m).digits.sum == n} }
a131382.take(70).each_slice(10){|slice| puts "%8d"*slice.size % slice }</syntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 1
19 19 4 19 19 13 28 28 11 46
199 19 109 73 37 199 73 37 271 172
1333 289 559 1303 847 1657 833 1027 1576 1282
17497 4339 2119 2323 10909 11111 12826 14617 14581 16102
199999 17449 38269 56413 37037 1108909 142498 103507 154981 150661
1333333 163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program minimum_multiple_of_m_where_digit_sum_equals_m;
loop for n in [1..70] do
putchar(lpad(str minmult n, 9));
if n mod 10=0 then print; end if;
end loop;
 
op minmult(m);
n := 1;
(while digsum(n*m) /= m) n +:= 1; end;
return n;
end op;
 
op digsum(n);
return +/[[n mod 10, n div:=10](1) : until n=0];
end op;
end program;</syntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var e = Enumerator({|f|
for n in (1..Inf) {
 
var k = 0
while (k.sumdigits != n) {
k += n
}
 
f(k/n)
}
})
 
var N = 60
var A = []
 
e.each {|v|
A << v
say A.splice.map { '%7s' % _ }.join(' ') if (A.len == 10)
break if (--N <= 0)
}</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
</pre>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
 
func digitSum(_ num: Int) -> Int {
var sum = 0
var n = num
while n > 0 {
sum += n % 10
n /= 10
}
return sum
}
 
for n in 1...70 {
for m in 1... {
if digitSum(m * n) == n {
print(String(format: "%8d", m), terminator: n % 10 == 0 ? "\n" : " ")
break
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
4339 2119 2323 10909 11111 12826 14617 14581 16102 199999
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|Wren}}==
Line 1,016 ⟶ 2,567:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
Line 1,026 ⟶ 2,577:
res.add(m)
}
for (chunk in Lst.chunks(res, 10)) Fmt.printtprint("$,10d", chunkres, 10)</langsyntaxhighlight>
 
{{out}}
Line 1,040 ⟶ 2,591:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func SumDigits(N); \Return sum of digits in N
int N, S;
[S:= 0;
Line 1,059 ⟶ 2,610:
N:= N+1;
until C >= 40+30;
]</langsyntaxhighlight>
 
{{out}}
2,093

edits