Extreme primes: Difference between revisions

(→‎{{header|Go}}: Some changes as a result of changes to Go-rcu package.)
Line 240:
 
This would be more efficient if we had used 3e3 (216 extreme primes) rather than 4e4 (1942 extreme primes)
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with both jq and gojq, the C and Go implementations of jq''' ...
 
'''Generic Utilities'''
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else
($n | sqrt) as $rt
| 23
| until( . > $rt or ($n % . == 0); .+2)
| . > $rt
end;
 
def nextprime:
(if .%2==0 then 1 else 2 end) as $n
| first(range(.+$n; infinite;2) | select(is_prime));
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang=jq>
{ extremes:[2],
sum:2,
p:3,
count:1 }
| while (.count <= 5000;
.emit = null
| .sum += .p
| if .sum|is_prime
then .count += 1
| if .count <= 30
then .extremes += [.sum]
| if .count == 30
then .emit = "The first 30 extreme primes are:\n"
| .emit += ([.extremes | nwise(10) | map(lpad(7)) | join(" ") ] | join("\n"))
| .emit += "\n"
else .
end
elif .count % 1000 == 0
then .emit = "The \(.count)th extreme prime is: \(.sum) for p <= \(.p)"
else .
end
else .
end
| .p |= nextprime
)
| .emit // empty
</syntaxhighlight>
{{output}}
<pre>
The first 30 extreme primes are:
2 5 17 41 197 281 7699 8893 22039 24133
25237 28697 32353 37561 38921 43201 44683 55837 61027 66463
70241 86453 102001 109147 116533 119069 121631 129419 132059 263171
 
The 1000th extreme prime is: 1657620079 for p <= 196831
The 2000th extreme prime is: 9744982591 for p <= 495571
The 3000th extreme prime is: 24984473177 for p <= 808837
The 4000th extreme prime is: 49394034691 for p <= 1152763
The 5000th extreme prime is: 82195983953 for p <= 1500973
<pre>
 
=={{header|Julia}}==
2,442

edits