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

Add ABC
(Added Go)
(Add ABC)
 
(21 intermediate revisions by 15 users not shown)
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">
<lang AWK>
# syntax: GAWK -f MINIMUM_MULTIPLE_OF_M_WHERE_DIGITAL_SUM_EQUALS_M.AWK
BEGIN {
Line 95 ⟶ 149:
return(s)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 110 ⟶ 164:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">c = 0
n = 1
while c < 70
Line 129 ⟶ 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 156 ⟶ 256:
n += 1
Loop
Sleep</syntaxhighlight>
</lang>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 166 ⟶ 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 187 ⟶ 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 216 ⟶ 429:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 227 ⟶ 440:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 246 ⟶ 459:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 260 ⟶ 473:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">digit_sum = proc (n: int) returns (int)
sum: int := 0
while n > 0 do
Line 291 ⟶ 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 302 ⟶ 515:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. A131382.
Line 343 ⟶ 556:
ADD-DIGIT.
ADD DIGITS(D) TO DIGITSUM.</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>A( 1) = 1
Line 387 ⟶ 600:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub digit_sum(n: uint32): (sum: uint8) is
Line 411 ⟶ 624:
end if;
n := n + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre>1 1 1 1 1 1 1 1 1 19
Line 422 ⟶ 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 455 ⟶ 668:
if (n & 7) = 0 then writeln() fi
od
corp</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1
Line 466 ⟶ 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#}}==
<langsyntaxhighlight 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
Line 477 ⟶ 764:
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>
</lang>
{{out}}
<pre>
Line 488 ⟶ 775:
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 F N=1,40;D 2
01.20 Q
 
Line 503 ⟶ 791:
03.30 S S=S+(D-E*10)
03.40 S D=E
03.50 I (-D)3.2</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>N= 1 M= 1
Line 549 ⟶ 837:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: digit-sum ( u -- u )
dup 10 < if exit then
10 /mod recurse + ;
Line 563 ⟶ 851:
 
70 main
bye</langsyntaxhighlight>
 
{{out}}
Line 578 ⟶ 866:
=={{header|Go}}==
{{libheader|Go-rcu}}
<langsyntaxhighlight Golang="go">package main
 
import "rcu"
Line 592 ⟶ 880:
}
rcu.PrintTable(res, 7, 10, true)
}</langsyntaxhighlight>
 
{{out}}
Line 609 ⟶ 897:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.List (elemIndex, intercalate, transpose)
import Data.List.Split (chunksOf)
Line 640 ⟶ 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 651 ⟶ 939:
Implementation:
 
<syntaxhighlight lang="j">
<lang J>
findfirst=: {{
($:@((+1+i.@+:)@#)@[`(+&{. I.)@.(1 e. ]) u) ,1
Line 658 ⟶ 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 694 ⟶ 1,113:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
VECTOR VALUES FMT = $2HA(,I2,4H) = ,I8*$
Line 711 ⟶ 1,130:
TRANSFER TO DIGIT
END OF FUNCTION
END OF PROGRAM</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>A( 1) = 1
Line 783 ⟶ 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}}==
Line 789 ⟶ 1,242:
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.
<langsyntaxhighlight lang="pascal">program m_by_n_sumofdgts_m;
//Like https://oeis.org/A131382/b131382.txt
{$IFDEF FPC} {$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$ENDIF}
Line 1,014 ⟶ 1,467:
writeln('Total runtime ',GetTickCount64-T0,' ms');
{$IFDEF WINDOWS} readln{$ENDIF}
end.</langsyntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Line 1,083 ⟶ 1,536:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Minimum_multiple_of_m_where_digital_sum_equals_m
Line 1,095 ⟶ 1,548:
$m;
} 1 .. 70;
print "@answers\n\n" =~ s/.{65}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,108 ⟶ 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 1,126 ⟶ 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 1,141 ⟶ 1,594:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/mmnn.htm here].
<!--<langsyntaxhighlight Phixlang="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>
Line 1,497 ⟶ 1,950:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,577 ⟶ 2,030:
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.
<!--<langsyntaxhighlight Phixlang="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>
Line 1,638 ⟶ 2,091:
<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>
<!--</langsyntaxhighlight>-->
{{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
Line 1,650 ⟶ 2,103:
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 1,683 ⟶ 2,216:
divmod(N, 10, M, Digit),
S2 is S1 + Digit,
digit_sum(M, Sum, S2).</langsyntaxhighlight>
 
{{out}}
Line 1,694 ⟶ 2,227:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''A131382'''
 
from itertools import count, islice
Line 1,784 ⟶ 2,317:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 1,793 ⟶ 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,867 ⟶ 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}}==
<langsyntaxhighlight lang="ruby">var e = Enumerator({|f|
for n in (1..Inf) {
 
Line 1,888 ⟶ 2,519:
say A.splice.map { '%7s' % _ }.join(' ') if (A.len == 10)
break if (--N <= 0)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,900 ⟶ 2,531:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func digitSum(_ num: Int) -> Int {
Line 1,919 ⟶ 2,550:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,936 ⟶ 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,946 ⟶ 2,577:
res.add(m)
}
for (chunk in Lst.chunks(res, 10)) Fmt.printtprint("$,10d", chunkres, 10)</langsyntaxhighlight>
 
{{out}}
Line 1,960 ⟶ 2,591:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func SumDigits(N); \Return sum of digits in N
int N, S;
[S:= 0;
Line 1,979 ⟶ 2,610:
N:= N+1;
until C >= 40+30;
]</langsyntaxhighlight>
 
{{out}}
2,114

edits