Idoneal numbers: Difference between revisions

(Added XPL0 example.)
Line 211:
</syntaxhighlight>
Here, <code>comb</code> gives us all combinations of 3 numbers in ascending order in the given range (originally in the range 0..254 here, adding 1 shifts them to 1..255). Then we multiply all pairs from these combinations, sum the resulting products and remove those products from a sequence 1..255. (And we form the result into rows of 10 numbers each, to avoid an excessively long row of numbers.)
 
=={{header|jq}}==
This entry uses jq's `break` because the equivalent program using `until` is much slower.
 
Using either the C or Go implementations of jq,
the program shown below takes about 5 seconds to produce the 65
idoneal numbers.
For gojq, the definition of the `_nwise` helper function must be
uncommented.
<syntaxhighlight lang=jq>
# For gojq:
# def _nwise($n):
# def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
# n;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def isIdoneal:
first(
label $out
| . as $n
| range(1; $n) as $a
| label $startB
| range($a+1; $n) as $b
| ($a+$b) as $sum
| ($a*$b) as $prod
| if $prod + $sum > $n then break $startB
else label $startC
| range($b+1; $n) as $c
| ($prod + $sum*$c) as $x
| if $x == $n then 0, break $out
elif $x > $n then break $startC
else empty
end
end )
// true | if . == 0 then false else . end;
 
# Search blindly
def idoneals: range(1; infinite) | select(isIdoneal);
 
# The task:
[limit(65; idoneals)]
| _nwise(13) | map(lpad(5)) | join(" ")
</syntaxhighlight>
Invocation: jq -nr -f idoneal.jq
{{output}}
<pre>
1 2 3 4 5 6 7 8 9 10 12 13 15
16 18 21 22 24 25 28 30 33 37 40 42 45
48 57 58 60 70 72 78 85 88 93 102 105 112
120 130 133 165 168 177 190 210 232 240 253 273 280
312 330 345 357 385 408 462 520 760 840 1320 1365 1848
</pre>
 
=={{header|PARI/GP}}==
2,442

edits