Frobenius numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: optimized the GENP subroutine.)
Line 763: Line 763:
770879 776159* 781451 802715 824459* 835379* 851903 868607 879839* 889239
770879 776159* 781451 802715 824459* 835379* 851903 868607 879839* 889239
900591 919631* 937019 946719 958431 972179 986039
900591 919631* 937019 946719 958431 972179 986039
</pre>

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

The solution offered here is based on a function that can generate an unbounded stream of Frobenius numbers without relying on the precomputation or storage of an array of primes except as may be used by `is_prime`.

See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<lang jq># Generate a stream of Frobenius numbers up to an including `.`;
# specify `null` or `infinite` to generate an unbounded stream.
def frobenius:
. as $limit
| label $out
| foreach (range(3;infinite;2) | select(is_prime)) as $p ({p: null, prev: 2};
.p = $p
| (.prev * $p - .prev - $p) as $frob
| if $limit == null or $frob > $limit then break $out
else .frob = $frob
end
| .prev = $p;
.frob);

10001 | frobenius</lang>
{{out}}
<pre>
1
7
23
59
119
191
287
395
615
839
1079
1439
1679
1931
2391
3015
3479
3959
4619
5039
5615
6395
7215
8447
9599
</pre>
</pre>