Jump to content

Prime triplets: Difference between revisions

m (→‎{{header|Perl}}: note use of 'theory' module)
Line 307:
1030 IF I*I > P THEN RETURN
1040 GOTO 1010</lang>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The implementation of `is_prime` at [[Erd%C5%91s-primes#jq]] can be used here and is therefore not repeated.
 
The `prime_triplets` defined here generates an unbounded stream of prime triples, which is harnessed by the generic function `emit_until` defined as follows:
<lang jq>
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;</lang>
The Task:
<lang jq># Output: [p,p+2,p+6] where p is prime
def prime_triplets:
def pt: .[2] == .[1] + 4 and .[1] == .[0] + 2;
def next: .[1:] + [first( range(.[2] + 2; infinite;2) | select(is_prime))];
# prime the foreach with the first triplet
foreach range(7; infinite; 2) as $i ([2,3,5]; next; select(pt) ) ;
 
emit_until(.[0] >= 5500; prime_triplets) </lang>
{{out}}
<pre>
[5,7,11]
[11,13,17]
[17,19,23]
[41,43,47]
[101,103,107]
[107,109,113]
[191,193,197]
[227,229,233]
[311,313,317]
[347,349,353]
[461,463,467]
[641,643,647]
[821,823,827]
[857,859,863]
[881,883,887]
[1091,1093,1097]
[1277,1279,1283]
[1301,1303,1307]
[1427,1429,1433]
[1481,1483,1487]
[1487,1489,1493]
[1607,1609,1613]
[1871,1873,1877]
[1997,1999,2003]
[2081,2083,2087]
[2237,2239,2243]
[2267,2269,2273]
[2657,2659,2663]
[2687,2689,2693]
[3251,3253,3257]
[3461,3463,3467]
[3527,3529,3533]
[3671,3673,3677]
[3917,3919,3923]
[4001,4003,4007]
[4127,4129,4133]
[4517,4519,4523]
[4637,4639,4643]
[4787,4789,4793]
[4931,4933,4937]
[4967,4969,4973]
[5231,5233,5237]
[5477,5479,5483]
</pre>
 
=={{header|Julia}}==
2,497

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.