Minimum multiple of m where digital sum equals m: Difference between revisions
Minimum multiple of m where digital sum equals m (view source)
Revision as of 22:41, 27 August 2022
, 1 year agosyntax highlighting fixup automation
(Added Go) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 23:
=={{header|ALGOL 68}}==
<
# returns the digit sum of n, n must be >= 0 #
OP DIGITSUM = ( INT n )INT:
Line 47:
OD
OD
END</
{{out}}
<pre>
Line 62:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{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">
# syntax: GAWK -f MINIMUM_MULTIPLE_OF_M_WHERE_DIGITAL_SUM_EQUALS_M.AWK
BEGIN {
Line 95:
return(s)
}
</syntaxhighlight>
{{out}}
<pre>
Line 110:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<
n = 1
while c < 70
Line 129:
n += 1
end while
end</
{{out}}
<pre>
Line 137:
==={{header|FreeBASIC}}===
{{trans|Phix}}
<
Dim As Integer c = 0, n = 1
Line 157:
Loop
Sleep
</syntaxhighlight>
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 168:
==={{header|Yabasic}}===
<
n = 1
while c < 70
Line 187:
n = n + 1
wend
end</
{{out}}
<pre>
Line 194:
=={{header|C}}==
<
unsigned digit_sum(unsigned n) {
Line 216:
}
return 0;
}</
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 227:
=={{header|C++}}==
<
#include <iostream>
Line 246:
}
}
}</
{{out}}
Line 260:
=={{header|CLU}}==
<
sum: int := 0
while n > 0 do
Line 291:
if n // 10 = 0 then stream$putl(po, "") end
end
end start_up</
{{out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 302:
=={{header|COBOL}}==
<
PROGRAM-ID. A131382.
Line 343:
ADD-DIGIT.
ADD DIGITS(D) TO DIGITSUM.</
{{out}}
<pre style='height:50ex;'>A( 1) = 1
Line 387:
=={{header|Cowgol}}==
<
sub digit_sum(n: uint32): (sum: uint8) is
Line 411:
end if;
n := n + 1;
end loop;</
{{out}}
<pre>1 1 1 1 1 1 1 1 1 19
Line 422:
=={{header|Draco}}==
<
* 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</
{{out}}
<pre> 1 1 1 1 1 1 1 1
Line 468:
=={{header|F_Sharp|F#}}==
<
// 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>
{{out}}
<pre>
Line 489:
</pre>
=={{header|FOCAL}}==
<
01.20 Q
Line 503:
03.30 S S=S+(D-E*10)
03.40 S D=E
03.50 I (-D)3.2</
{{out}}
<pre style='height:50ex;'>N= 1 M= 1
Line 549:
=={{header|Forth}}==
{{works with|Gforth}}
<
dup 10 < if exit then
10 /mod recurse + ;
Line 563:
70 main
bye</
{{out}}
Line 578:
=={{header|Go}}==
{{libheader|Go-rcu}}
<
import "rcu"
Line 592:
}
rcu.PrintTable(res, 7, 10, true)
}</
{{out}}
Line 609:
=={{header|Haskell}}==
<
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</
{{Out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 651:
Implementation:
<syntaxhighlight lang="j">
findfirst=: {{
($:@((+1+i.@+:)@#)@[`(+&{. I.)@.(1 e. ]) u) ,1
Line 658:
A131382=: {{y&{{x = sumdigits x*y}} findfirst}}"0
sumdigits=: +/@|:@(10&#.inv)</
Task example:
<
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</
Stretch example:
<
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</
=={{header|Julia}}==
<
for j in 1:70
print(lpad(minproddigsum(j), 10), j % 7 == 0 ? "\n" : "")
end
</
<pre>
1 1 1 1 1 1 1
Line 694:
=={{header|MAD}}==
<
VECTOR VALUES FMT = $2HA(,I2,4H) = ,I8*$
Line 711:
TRANSFER TO DIGIT
END OF FUNCTION
END OF PROGRAM</
{{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.
<
//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.</
{{out|@TIO.RUN}}
<pre>
Line 1,083:
=={{header|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;</
{{out}}
<pre>
Line 1,108:
=={{header|Phix}}==
{{trans|XPL0}}
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,141:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/mmnn.htm here].
<!--<
<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>
<!--</
{{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.
<!--<
<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>
<!--</
{{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}}
<
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).</
{{out}}
Line 1,694:
=={{header|Python}}==
<
from itertools import count, islice
Line 1,784:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre> 1 1 1 1 1 1 1 1 1 19
Line 1,793:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
say .fmt("%2d: ") ~ .&min-mult-dsum for flat 1..40, 41..70;</
{{out}}
<pre> 1: 1
Line 1,869:
=={{header|Sidef}}==
<
for n in (1..Inf) {
Line 1,888:
say A.splice.map { '%7s' % _ }.join(' ') if (A.len == 10)
break if (--N <= 0)
}</
{{out}}
<pre>
Line 1,900:
=={{header|Swift}}==
<
func digitSum(_ num: Int) -> Int {
Line 1,919:
}
}
}</
{{out}}
Line 1,936:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
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)</
{{out}}
Line 1,960:
=={{header|XPL0}}==
<
int N, S;
[S:= 0;
Line 1,979:
N:= N+1;
until C >= 40+30;
]</
{{out}}
|