Sexy primes: Difference between revisions

m (→‎{{header|J}}: bugfix)
Line 1,324:
[999853], [999863], [999883], [999907], [999917], [999931], [999961], [999979], [999983], [1000003]
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
''Also works with gojq and fq''
 
See e.g. [[Anaprimes#jq]] for a suitable jq def of `primeSieve`.
<syntaxhighlight lang=jq>
include "primeSieve"; # or copy-and-paste its def
 
def when(filter; action): if filter // null then action else . end;
 
def results($cat; $lim; $max; $array):
($array|length) as $len
| (if $cat != "unsexy primes" then "sexy prime " + $cat else $cat end) as $cat
| (if $len < $max then $len else $max end) as $last
| (if $last == 1 then "is" else "are" end) as $verb
| "Number of \($cat) less than \($lim) = \($len)",
"The last \($max) \($verb):\n \($array[ - $last :])\n";
 
def task($lim):
(($lim-1) | primeSieve) as $sieve # $sieve[i] iff i is prime
| { pairs:[], trips:[], quads:[], quins:[], unsexy:[2, 3], i: 3 }
| until (.i >= $lim;
if .i > 5 and .i < $lim-6 and $sieve[.i] and ($sieve[.i-6]|not) and ($sieve[.i+6] | not)
then .unsexy += [.i]
else when(.i < $lim-6 and $sieve[.i] and $sieve[.i+6];
.pairs += [[.i, .i+6]]
| when(.i < $lim-12 and $sieve[.i+12];
.trips += [[.i, .i+6, .i+12]]
| when(.i < $lim-18 and $sieve[.i+18];
.quads += [[.i, .i+6, .i+12, .i+18]]
| when(.i < $lim-24 and $sieve[.i+24];
.quins += [[.i, .i+6, .i+12, .i+18, .i+24]]))))
end
| .i += 2 )
| results("pairs"; $lim; 5; .pairs),
results("triplets"; $lim; 5; .trips),
results("quadruplets"; $lim; 5; .quads),
results("quintuplets"; $lim; 5; .quins),
results("unsexy primes"; $lim; 10; .unsexy)
;
 
task(1000035)
</syntaxhighlight>
{{output}}
<pre>
Number of sexy prime pairs less than 1000035 = 16386
The last 5 are:
[[999371,999377],[999431,999437],[999721,999727],[999763,999769],[999953,999959]]
 
Number of sexy prime triplets less than 1000035 = 2900
The last 5 are:
[[997427,997433,997439],[997541,997547,997553],[998071,998077,998083],[998617,998623,998629],[998737,998743,998749]]
 
Number of sexy prime quadruplets less than 1000035 = 325
The last 5 are:
[[977351,977357,977363,977369],[983771,983777,983783,983789],[986131,986137,986143,986149],[990371,990377,990383,990389],[997091,997097,997103,997109]]
 
Number of sexy prime quintuplets less than 1000035 = 1
The last 5 is:
[[5,11,17,23,29]]
 
Number of unsexy primes less than 1000035 = 48627
The last 10 are:
[999853,999863,999883,999907,999917,999931,999961,999979,999983,1000003]
</pre>
 
 
=={{header|Julia}}==
2,498

edits