Magnanimous numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Change to task status) |
|||
Line 915: | Line 915: | ||
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081 |
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081 |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
For a suitable definition of `is_prime`, see [[Erd%C5%91s-primes#jq]]. |
|||
'''Preliminaries''' |
|||
<lang jq># To take advantage of gojq's arbitrary-precision integer arithmetic: |
|||
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
|||
def divrem($x; $y): |
|||
[$x/$y|floor, $x % $y]; |
|||
</lang> |
|||
'''The Task''' |
|||
<lang jq> |
|||
def ismagnanimous: |
|||
. as $n |
|||
| if $n < 10 then true |
|||
else first(range( 1; tostring|length) as $i |
|||
| divrem($n; (10|power($i))) as [$q, $r] |
|||
| if ($q + $r) | is_prime == false then 0 else empty end) |
|||
// true |
|||
| . == true |
|||
end; |
|||
# An unbounded stream ... |
|||
def magnanimous: |
|||
range(0; infinite) |
|||
| select(ismagnanimous); |
|||
[limit(400; magnanimous)] |
|||
| "First 45 magnanimous numbers:", .[:45], |
|||
"\n241st through 250th magnanimous numbers:", .[241:251], |
|||
"\n391st through 400th magnanimous numbers:", .[391:]</lang> |
|||
{{out}} |
|||
<pre> |
|||
First 45 magnanimous numbers: |
|||
[0,1,2,3,4,5,6,7,8,9,11,12,14,16,20,21,23,25,29,30,32,34,38,41,43,47,49,50,52,56,58,61,65,67,70,74,76,83,85,89,92,94,98,101,110] |
|||
241st through 250th magnanimous numbers: |
|||
[19972,20209,20261,20861,22061,22201,22801,22885,24407,26201] |
|||
391st through 400th magnanimous numbers: |
|||
[488489,515116,533176,551558,559952,595592,595598,600881,602081] |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |