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

m
syntax highlighting fixup automation
(Added Go)
m (syntax highlighting fixup automation)
Line 23:
 
=={{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:
OD
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 62:
=={{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 72:
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:
return(s)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 110:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">c = 0
n = 1
while c < 70
Line 129:
n += 1
end while
end</langsyntaxhighlight>
{{out}}
<pre>
Line 137:
==={{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 157:
Loop
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 168:
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">c = 0
n = 1
while c < 70
Line 187:
n = n + 1
wend
end</langsyntaxhighlight>
{{out}}
<pre>
Line 194:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
unsigned digit_sum(unsigned n) {
Line 216:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 227:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 246:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 260:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">digit_sum = proc (n: int) returns (int)
sum: int := 0
while n > 0 do
Line 291:
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:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. A131382.
Line 343:
ADD-DIGIT.
ADD DIGITS(D) TO DIGITSUM.</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>A( 1) = 1
Line 387:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub digit_sum(n: uint32): (sum: uint8) is
Line 411:
end if;
n := n + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre>1 1 1 1 1 1 1 1 1 19
Line 422:
 
=={{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:
if (n & 7) = 0 then writeln() fi
od
corp</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1
Line 468:
 
=={{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:
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 489:
</pre>
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 F N=1,40;D 2
01.20 Q
 
Line 503:
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:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: digit-sum ( u -- u )
dup 10 < if exit then
10 /mod recurse + ;
Line 563:
 
70 main
bye</langsyntaxhighlight>
 
{{out}}
Line 578:
=={{header|Go}}==
{{libheader|Go-rcu}}
<langsyntaxhighlight Golang="go">package main
 
import "rcu"
Line 592:
}
rcu.PrintTable(res, 7, 10, true)
}</langsyntaxhighlight>
 
{{out}}
Line 609:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.List (elemIndex, intercalate, transpose)
import Data.List.Split (chunksOf)
Line 640:
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:
Implementation:
 
<syntaxhighlight lang="j">
<lang J>
findfirst=: {{
($:@((+1+i.@+:)@#)@[`(+&{. I.)@.(1 e. ]) u) ,1
Line 658:
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|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:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
VECTOR VALUES FMT = $2HA(,I2,4H) = ,I8*$
Line 711:
TRANSFER TO DIGIT
END OF FUNCTION
END OF PROGRAM</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>A( 1) = 1
Line 789:
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:
writeln('Total runtime ',GetTickCount64-T0,' ms');
{$IFDEF WINDOWS} readln{$ENDIF}
end.</langsyntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Line 1,083:
 
=={{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:
$m;
} 1 .. 70;
print "@answers\n\n" =~ s/.{65}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,108:
=={{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:
<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:
{{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:
<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:
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:
<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,654:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">main:-
between(1, 40, N),
min_mult_dsum(N, M),
Line 1,683:
divmod(N, 10, M, Digit),
S2 is S1 + Digit,
digit_sum(M, Sum, S2).</langsyntaxhighlight>
 
{{out}}
Line 1,694:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''A131382'''
 
from itertools import count, islice
Line 1,784:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 1,793:
=={{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,869:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var e = Enumerator({|f|
for n in (1..Inf) {
 
Line 1,888:
say A.splice.map { '%7s' % _ }.join(' ') if (A.len == 10)
break if (--N <= 0)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,900:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func digitSum(_ num: Int) -> Int {
Line 1,919:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,936:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
Line 1,946:
res.add(m)
}
for (chunk in Lst.chunks(res, 10)) Fmt.print("$,10d", chunk)</langsyntaxhighlight>
 
{{out}}
Line 1,960:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func SumDigits(N); \Return sum of digits in N
int N, S;
[S:= 0;
Line 1,979:
N:= N+1;
until C >= 40+30;
]</langsyntaxhighlight>
 
{{out}}
10,333

edits