Disarium numbers: Difference between revisions

Content added Content deleted
(Added Algol 60)
Line 1,140: Line 1,140:
<pre>
<pre>
1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
</pre>

=={{header|jq}}==
{{Works with|jq}}
'''Also works with gojq, the Go implementation of jq'''

The naive algorithm is good enough to find the first 19 disarium numbers, ergo `limit(19; ...)` below.
<syntaxhighlight lang=jq>
# To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($in;$b): reduce range(0;$b) as $i (1; . * $in);

# $n is assumed to be a non-negative integer
def is_disarium:
. as $n
| {$n, sum: 0, len: (tostring|length) }
| until (.n == 0;
.sum += power(.n % 10; .len)
| .n = (.n/10 | floor)
| .len -= 1 )
| .sum == $n ;

# Emit a stream ...
def disariums:
range(0; infinite) | select(is_disarium);

limit(19; disariums)
</syntaxhighlight>
{{output}}
<pre>
0
...
2646798
</pre>
</pre>


Line 1,161: Line 1,193:
0.555962 seconds (5.29 M allocations: 562.335 MiB, 10.79% gc time)
0.555962 seconds (5.29 M allocations: 562.335 MiB, 10.79% gc time)
</pre>
</pre>



=={{header|Kotlin}}==
=={{header|Kotlin}}==