Prime numbers p for which the sum of primes less than or equal to p is prime: Difference between revisions

Line 208:
Total such primes < 1000: 21
</pre>
 
=={{header|Nim}}==
<lang Nim>import strutils, sugar
 
const
N = 1000 - 1 # Maximum value for prime.
S = N * (N + 1) div 2 # Maximum value for sum.
 
var composite: array[2..S, bool]
for n in 2..S:
let n2 = n * n
if n2 > S: break
if not composite[n]:
for k in countup(n2, S, n):
composite[k] = true
 
template isPrime(n: int): bool = not composite[n]
 
let primes = collect(newSeq):
for n in 2..N:
if n.isPrime: n
 
var list: seq[int]
var sum = 0
for p in primes:
sum += p
if sum.isPrime:
list.add p
 
echo "Found $# primes:".format(list.len)
echo list.join(" ")</lang>
 
{{out}}
<pre>Found 21 primes:
2 3 7 13 37 43 281 311 503 541 557 593 619 673 683 733 743 839 881 929 953</pre>
 
=={{header|Phix}}==
Anonymous user