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

(Created Nim solution.)
Line 820:
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'''
 
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; $max):
foreach (stream, nan) as $max ([];
if length == $max then [$x] else . + [$x] end;
if (.[-1] | isnan) and length>1 then .[:-1]
elif length == $max 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}}==
2,467

edits