Summarize primes: Difference between revisions

New post.
(New post.)
 
(42 intermediate revisions by 19 users not shown)
Line 1:
{{Draft taskTask}}
 
;Task:
 
Summarize first n primes (p) and check if it is a prime, where '''p < 1000'''
Considering in order of length, n, all sequences of consecutive
primes, p, from 2 onwards, where p < 1000 and n>0, select those
sequences whose sum is prime, and for these display the length of the
sequence, the last item in the sequence, and the sum.
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
I a < 2 | a % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(a))).step(2)
I a % i == 0
R 0B
R 1B
 
print(‘index prime prime sum’)
V s = 0
V idx = 0
L(n) 2..999
I is_prime(n)
idx++
s += n
I is_prime(s)
print(f:‘{idx:3} {n:5} {s:7}’)</syntaxhighlight>
 
{{out}}
<pre>
index prime prime sum
1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241
</pre>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">BEGIN # sum the primes below n and report the sums that are prime #
# sieve the primes to 999 #
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 999;
# sum the primes and test the sum #
INT prime sum := 0;
INT prime count := 0;
INT prime sum count := 0;
print( ( "prime prime", newline ) );
print( ( "count prime sum", newline ) );
FOR i TO UPB prime DO
IF prime[ i ] THEN
# have another prime #
prime count +:= 1;
prime sum +:= i;
# check whether the prime sum is prime or not #
BOOL is prime := TRUE;
FOR p TO i OVER 2 WHILE is prime DO
IF prime[ p ] THEN is prime := prime sum MOD p /= 0 FI
OD;
IF is prime THEN
# the prime sum is also prime #
prime sum count +:= 1;
print( ( whole( prime count, -5 )
, " "
, whole( i, -6 )
, " "
, whole( prime sum, -6 )
, newline
)
)
FI
FI
OD;
print( ( newline
, "Found "
, whole( prime sum count, 0 )
, " prime sums of primes below "
, whole( UPB prime + 1, 0 )
, newline
)
)
END</syntaxhighlight>
{{out}}
<pre>
prime prime
count prime sum
1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241
 
Found 21 prime sums of primes below 1000
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin % sum the primes below n and report the sums that are prime %
integer MAX_NUMBER;
MAX_NUMBER := 999;
begin
logical array prime( 1 :: MAX_NUMBER );
integer primeCount, primeSum, primeSumCount;
% sieve the primes to MAX_NUMBER %
prime( 1 ) := false; prime( 2 ) := true;
for i := 3 step 2 until MAX_NUMBER do prime( i ) := true;
for i := 4 step 2 until MAX_NUMBER do prime( i ) := false;
for i := 3 step 2 until truncate( sqrt( MAX_NUMBER ) ) do begin
integer ii; ii := i + i;
if prime( i ) then begin
for p := i * i step ii until MAX_NUMBER do prime( p ) := false
end if_prime_i
end for_i ;
% find the prime sums that are prime %
primeCount := primeSum := primeSumCount := 0;
write( "prime prime" );
write( "count sum" );
for i := 1 until MAX_NUMBER do begin
if prime( i ) then begin
% have another prime %
logical isPrime;
primeSum := primeSum + i;
primeCount := primeCount + 1;
% check whether the prime sum is also prime %
isPrime := true;
for p := 1 until i div 2 do begin
if prime( p ) then begin
isPrime := primeSum rem p not = 0;
if not isPrime then goto endPrimeCheck
end if_prime_p
end for_p ;
endPrimeCheck:
if isPrime then begin
% the prime sum is also prime %
primeSumCount := primeSumCount + 1;
write( i_w := 5, s_w := 0
, primeCount
, " "
, i_w := 6
, primeSum
)
end if_isPrime
end if_prime_i
end for_i ;
write();
write( i_w := 1, s_w := 0
, "Found "
, primeSumCount
, " prime sums of primes below "
, MAX_NUMBER + 1
)
end
end.</syntaxhighlight>
{{out}}
<pre>
prime prime
count sum
1 2
2 5
4 17
6 41
12 197
14 281
60 7699
64 8893
96 22039
100 24133
102 25237
108 28697
114 32353
122 37561
124 38921
130 43201
132 44683
146 55837
152 61027
158 66463
162 70241
 
Found 21 prime sums of primes below 1000
</pre>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print (pad "index" 6) ++ " | " ++
(pad "prime" 6) ++ " | " ++
(pad "prime sum" 11)
Line 23 ⟶ 236:
(pad to :string s 11)
]
]</langsyntaxhighlight>
 
{{out}}
Line 52 ⟶ 265:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUMMARIZE_PRIMES.AWK
BEGIN {
Line 81 ⟶ 294:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 107 ⟶ 320:
Summarized primes 1-999: 21
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">#include "isprime.kbs"
 
print 1, 2, 2
sum = 2
n = 1
for i = 3 to 999 step 2
if isPrime(i) then
sum += i
n += 1
if isPrime(sum) then
print n, i, sum
end if
end if
next i</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#include "isprime.bas"
 
print 1,2,2
dim as integer sum = 2, i, n=1
for i = 3 to 999 step 2
if isprime(i) then
sum += i
n+=1
if isprime(sum) then
print n, i, sum
end if
end if
next i</syntaxhighlight>
{{out}}
<pre>
1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241</pre>
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Use "isprime.bas"
 
Public Sub Main()
Print 1, 2, 2
Dim n As Integer = 1, i As Integer, sum As Integer = 2
For i = 3 To 999 Step 2
If isPrime(i) Then
sum += i
n += 1
If isPrime(sum) Then
Print n, i, sum
End If
End If
Next
 
End</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="vb">;XIncludeFile "isprime.pb"
 
OpenConsole()
Define.i sum, i, n
PrintN("1" + #TAB$ + "2" + #TAB$ + "2")
sum = 2
n = 1
For i = 3 To 999 Step 2
If isPrime(i):
sum + i
n + 1
If isPrime(sum):
PrintN(Str(n) + #TAB$ + Str(i) + #TAB$ + Str(sum))
EndIf
EndIf
Next i
 
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">//import isprime
print 1, chr$(9), 2, chr$(9), 2
sum = 2
n = 1
for i = 3 to 999 step 2
if isPrime(i) then
sum = sum + i
n = n + 1
if isPrime(sum) print n, chr$(9), i, chr$(9), sum
fi
next i
end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C}}==
{{trans|C++}}
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
bool is_prime(int n) {
int i = 5;
 
if (n < 2) {
return false;
}
 
if (n % 2 == 0) {
return n == 2;
}
if (n % 3 == 0) {
return n == 3;
}
 
while (i * i <= n) {
if (n % i == 0) {
return false;
}
i += 2;
 
if (n % i == 0) {
return false;
}
i += 4;
}
 
return true;
}
 
int main() {
const int start = 1;
const int stop = 1000;
 
int sum = 0;
int count = 0;
int sc = 0;
int p;
 
for (p = start; p < stop; p++) {
if (is_prime(p)) {
count++;
sum += p;
if (is_prime(sum)) {
printf("The sum of %3d primes in [2, %3d] is %5d which is also prime\n", count, p, sum);
sc++;
}
}
}
printf("There are %d summerized primes in [%d, %d)\n", sc, start, stop);
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
The sum of 2 primes in [2, 3] is 5 which is also prime
The sum of 4 primes in [2, 7] is 17 which is also prime
The sum of 6 primes in [2, 13] is 41 which is also prime
The sum of 12 primes in [2, 37] is 197 which is also prime
The sum of 14 primes in [2, 43] is 281 which is also prime
The sum of 60 primes in [2, 281] is 7699 which is also prime
The sum of 64 primes in [2, 311] is 8893 which is also prime
The sum of 96 primes in [2, 503] is 22039 which is also prime
The sum of 100 primes in [2, 541] is 24133 which is also prime
The sum of 102 primes in [2, 557] is 25237 which is also prime
The sum of 108 primes in [2, 593] is 28697 which is also prime
The sum of 114 primes in [2, 619] is 32353 which is also prime
The sum of 122 primes in [2, 673] is 37561 which is also prime
The sum of 124 primes in [2, 683] is 38921 which is also prime
The sum of 130 primes in [2, 733] is 43201 which is also prime
The sum of 132 primes in [2, 743] is 44683 which is also prime
The sum of 146 primes in [2, 839] is 55837 which is also prime
The sum of 152 primes in [2, 881] is 61027 which is also prime
The sum of 158 primes in [2, 929] is 66463 which is also prime
The sum of 162 primes in [2, 953] is 70241 which is also prime
There are 21 summerized primes in [1, 1000)</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
 
bool is_prime(int n) {
Line 160 ⟶ 571:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 184 ⟶ 595:
The sum of 162 primes in [2, 953] is 70241 which is also prime
There are 21 summerized primes in [1, 1000)</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses the [[Extensible_prime_generator#Delphi|Delphi Prime-Generator Object]]
 
 
<syntaxhighlight lang="Delphi">
procedure SumOfPrimeSequences(Memo: TMemo);
var Sieve: TPrimeSieve;
var I,Inx, Sum: integer;
begin
Sieve:=TPrimeSieve.Create;
try
Sieve.Intialize(100000);
Memo.Lines.Add(' I P(I) Sum');
Memo.Lines.Add('---------------');
I:=0;
Sum:=0;
while Sieve.Primes[I]<1000 do
begin
Sum:=Sum+Sieve.Primes[I];
if Sieve.Flags[Sum] then
begin
Memo.Lines.Add(Format('%3d %4d %6d',[I,Sieve.Primes[I],Sum]));
end;
Inc(I,1);
end;
 
finally Sieve.Free; end;
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
I P(I) Sum
---------------
0 2 2
1 3 5
3 7 17
5 13 41
11 37 197
13 43 281
59 281 7699
63 311 8893
95 503 22039
99 541 24133
101 557 25237
107 593 28697
113 619 32353
121 673 37561
123 683 38921
129 733 43201
131 743 44683
145 839 55837
151 881 61027
157 929 66463
161 953 70241
 
Elapsed Time: 31.759 ms.
 
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func prime n .
if n mod 2 = 0 and n > 2
return 0
.
i = 3
while i <= sqrt n
if n mod i = 0
return 0
.
i += 2
.
return 1
.
for i = 2 to 999
if prime i = 1
ind += 1
sum += i
if prime sum = 1
print ind & ": " & sum
.
.
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Summarize Primes: Nigel Galloway. April 16th., 2021
primes32()|>Seq.takeWhile((>)1000)|>Seq.scan(fun(n,g) p->(n+1,g+p))(0,0)|>Seq.filter(snd>>isPrime)|>Seq.iter(fun(n,g)->printfn "%3d->%d" n g)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 219 ⟶ 720:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: assocs formatting kernel math.primes math.ranges
math.statistics prettyprint ;
 
1000 [ [1,b] ] [ primes-upto cum-sum ] bi zip
[ nip prime? ] assoc-filter
[ "The sum of the first %3d primes is %5d (which is prime).\n" printf ] assoc-each</langsyntaxhighlight>
{{out}}
<pre>
Line 251 ⟶ 752:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">n:=0
s:=0
for i=1, 162 do s:=s+Prime(i);if Isprime(s)=1 then n:=n+1;!!(n,Prime(i),s) fi od
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 282 ⟶ 783:
=={{header|Forth}}==
{{works with|Gforth}}
<syntaxhighlight lang ="forth">: prime? ( n -- ?flag ) here + c@ 0= ;
dup 2 < if drop false exit then
: notprime! ( n -- ) here + 1 swap c! ;
dup 2 mod 0= if 2 = exit then
 
dup 3 mod 0= if 3 = exit then
: prime_sieve { n -- }
5
here n erase
0 notprime!
1 notprime!
n 4 > if
n 4 do i notprime! 2 +loop
then
3
begin
dup2dup dup * n <>=
while
2dup mod 0= if 2drop false exit then
dup prime? if
n over dup * do
i notprime!
dup 2* +loop
then
2 +
2dup mod 0= if 2drop false exit then
4 +
repeat
drop2drop true ;
 
: main
0 0 { count sum }
." count prime sum" cr
100000 prime_sieve
1000 2 do
i prime? if
Line 320 ⟶ 812:
 
main
bye</langsyntaxhighlight>
 
{{out}}
<pre>count prime sum
<pre>
count prime sum
1 2 2
2 3 5
Line 345 ⟶ 836:
152 881 61027
158 929 66463
162 953 70241</pre>
</pre>
 
=={{header|FreeBASICFōrmulæ}}==
<lang freebasic>#include "isprime.bas"
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Summarize_primes}}
print 1,2,2
 
dim as integer sum = 2, i, n=1
'''Solution'''
for i = 3 to 999 step 2
 
if isprime(i) then
[[File:Fōrmulæ - Summarize primes 01.png]]
sum += i
 
n+=1
[[File:Fōrmulæ - Summarize primes 02.png]]
if isprime(sum) then
print n, i, sum
end if
end if
next i</lang>
{{out}}
<pre>
1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241</pre>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 411 ⟶ 873:
fmt.Println()
fmt.Println(c, "such prime sums found")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 418 ⟶ 880:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (scanl)
import Data.Numbers.Primes (isPrime, primes)
 
Line 436 ⟶ 898:
mapM_ print $
takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums
</syntaxhighlight>
</lang>
{{Out}}
<pre>(1,2,2)
Line 459 ⟶ 921:
(158,929,66463)
(162,953,70241)</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">primes=: p: i. _1 p: 1000 NB. all prime numbers below 1000
sums=: +/\ primes NB. running sum of those primes
mask=: 1 p: sums NB. array of 0s, 1s where sums are primes
 
NB. indices of prime sums (incremented for 1-based indexing)
NB. "copy" only the final primes in the prime sums
NB. "copy" only the sums which are prime
results=: (>: I. mask) ,. (mask # primes) ,. (mask # sums)
 
NB. pretty-printed "boxed" output
output=: 2 1 $ ' n prime sum ' ; < results</syntaxhighlight>
{{out}}
<pre> output
┌─────────────┐
│ n prime sum │
├─────────────┤
│ 1 2 2│
│ 2 3 5│
│ 4 7 17│
│ 6 13 41│
│ 12 37 197│
│ 14 43 281│
│ 60 281 7699│
│ 64 311 8893│
│ 96 503 22039│
│100 541 24133│
│102 557 25237│
│108 593 28697│
│114 619 32353│
│122 673 37561│
│124 683 38921│
│130 733 43201│
│132 743 44683│
│146 839 55837│
│152 881 61027│
│158 929 66463│
│162 953 70241│
└─────────────┘</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public final class SummarizePrimes {
 
public static void main(String[] args) {
final int start = 1;
final int finish = 1_000;
 
int sum = 0;
int count = 0;
int summarizedCount = 0;
 
for ( int p = start; p < finish; p++ ) {
if ( isPrime(p) ) {
count += 1;
sum += p;
if ( isPrime(sum) ) {
String word = ( count == 1 ) ? " prime" : " primes";
System.out.println(
"The sum of " + count + word + " in [2, " + p + "] is " + sum + ", which is also prime.");
summarizedCount++;
}
}
}
System.out.println(System.lineSeparator() +
"There are " + summarizedCount + " summarized primes in [" + start + ", " + finish + "].");
}
private static boolean isPrime(int number) {
if ( number < 2 ) {
return false;
}
if ( number % 2 == 0 ) {
return number == 2;
}
if ( number % 3 == 0 ) {
return number == 3;
}
 
int test = 5;
while ( test * test <= number ) {
if ( number % test == 0 ) {
return false;
}
test += 2;
if ( number % test == 0 ) {
return false;
}
test += 4;
}
return true;
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
The sum of 1 prime in [2, 2] is 2, which is also prime.
The sum of 2 primes in [2, 3] is 5, which is also prime.
The sum of 4 primes in [2, 7] is 17, which is also prime.
The sum of 6 primes in [2, 13] is 41, which is also prime.
The sum of 12 primes in [2, 37] is 197, which is also prime.
The sum of 14 primes in [2, 43] is 281, which is also prime.
The sum of 60 primes in [2, 281] is 7699, which is also prime.
The sum of 64 primes in [2, 311] is 8893, which is also prime.
The sum of 96 primes in [2, 503] is 22039, which is also prime.
The sum of 100 primes in [2, 541] is 24133, which is also prime.
The sum of 102 primes in [2, 557] is 25237, which is also prime.
The sum of 108 primes in [2, 593] is 28697, which is also prime.
The sum of 114 primes in [2, 619] is 32353, which is also prime.
The sum of 122 primes in [2, 673] is 37561, which is also prime.
The sum of 124 primes in [2, 683] is 38921, which is also prime.
The sum of 130 primes in [2, 733] is 43201, which is also prime.
The sum of 132 primes in [2, 743] is 44683, which is also prime.
The sum of 146 primes in [2, 839] is 55837, which is also prime.
The sum of 152 primes in [2, 881] is 61027, which is also prime.
The sum of 158 primes in [2, 929] is 66463, which is also prime.
The sum of 162 primes in [2, 953] is 70241, which is also prime.
 
There are 21 summarized primes in [1, 1000].
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">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 {i:23}
| until( (.i * .i) > $n or ($n % .i == 0); .i += 2)
| .i * .i > $n
end;
 
# primes up to but excluding $n
def primes($n): [range(2;$n) | select(is_prime)];
 
"Prime sums of primes less than 1000",
(primes(1000)
| range(1; length) as $n
| (.[: $n] | add) as $sum
| select($sum | is_prime)
| "The sum of the \($n) primes from 2 to \(.[$n-1]) is \($sum)." )</syntaxhighlight>
{{out}}
<pre>
Prime sums of primes less than 1000
The sum of the 1 primes from 2 to 2 is 2.
The sum of the 2 primes from 2 to 3 is 5.
The sum of the 4 primes from 2 to 7 is 17.
The sum of the 6 primes from 2 to 13 is 41.
The sum of the 12 primes from 2 to 37 is 197.
The sum of the 14 primes from 2 to 43 is 281.
The sum of the 60 primes from 2 to 281 is 7699.
The sum of the 64 primes from 2 to 311 is 8893.
The sum of the 96 primes from 2 to 503 is 22039.
The sum of the 100 primes from 2 to 541 is 24133.
The sum of the 102 primes from 2 to 557 is 25237.
The sum of the 108 primes from 2 to 593 is 28697.
The sum of the 114 primes from 2 to 619 is 32353.
The sum of the 122 primes from 2 to 673 is 37561.
The sum of the 124 primes from 2 to 683 is 38921.
The sum of the 130 primes from 2 to 733 is 43201.
The sum of the 132 primes from 2 to 743 is 44683.
The sum of the 146 primes from 2 to 839 is 55837.
The sum of the 152 primes from 2 to 881 is 61027.
The sum of the 158 primes from 2 to 929 is 66463.
The sum of the 162 primes from 2 to 953 is 70241.
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
p1000 = primes(1000)
Line 472 ⟶ 1,117:
end
end
</langsyntaxhighlight>{{out}}
<pre>
The sum of the 1 primes from prime 2 to prime 2 is 2, which is prime.
Line 496 ⟶ 1,141:
The sum of the 162 primes from prime 2 to prime 953 is 70241, which is prime.
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">p = Prime[Range[PrimePi[1000]]];
TableForm[
Select[Transpose[{Range[Length[p]], p, Accumulate[p]}], Last /* PrimeQ],
TableHeadings -> {None, {"Prime count", "Prime", "Prime sum"}}
]</syntaxhighlight>
{{out}}
<pre>Prime count Prime Prime sum
1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
const N = 999
Line 520 ⟶ 1,195:
inc idx
s += n
if s.isPrime: echo &"{idx:3} {n:5} {s:7}"</langsyntaxhighlight>
 
{{out}}
Line 548 ⟶ 1,223:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <nth_prime is_prime>;
Line 557 ⟶ 1,232:
} until $n >= $limit;
 
print "Of the first $limit primes: @{[scalar @sums]} cumulative prime sums:\n", join "\n", @sums;</langsyntaxhighlight>
{{out}}
<pre style="height:70ex">Of the first 1000 primes: 76 cumulative prime sums:
Line 639 ⟶ 1,314:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">sp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">))),</span><span style="color: #000000;">sp</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d of em: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Found 21 of em: 1, 2, 4, 6, 12, ..., 132, 146, 152, 158, 162
</pre>
=={{header|Prolog}}==
works with swi-prolog
<syntaxhighlight lang="prolog">isPrime(2).
isPrime(N):-
between(3, inf, N),
N /\ 1 > 0, % odd
M is floor(sqrt(N)) - 1, % reverse 2*I+1
Max is M div 2,
forall(between(1, Max, I), N mod (2*I+1) > 0).
 
primeSum([], _, _, []).
primeSum([P|PList], Index, Acc, [Index|CList]):-
Sum is Acc + P,
isPrime(Sum),!,
format('~|~t~d~3+ ~|~t~d~3+ ~|~t~d~5+', [Index, P, Sum]),nl,
Index1 is Index + 1,
primeSum(PList, Index1, Sum, CList).
primeSum([P|PList], Index, Acc, CntList):-
Index1 is Index + 1,
Sum is Acc + P,
primeSum(PList, Index1, Sum, CntList).
 
do:-Limit is 1000,
numlist(1, Limit, List),
include(isPrime, List, PrimeList),
primeSum(PrimeList, 1, 0, CntList),
length(CntList, Number),
format('~nfound ~d such primes~n', [Number]).</syntaxhighlight>
{{out}}
<pre>?- do.
1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241
 
found 21 such primes
</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Prime sums of primes up to 1000'''
 
 
Line 724 ⟶ 1,453:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>1 -> 2
Line 747 ⟶ 1,476:
158 -> 66463
162 -> 70241</pre>
 
=={{header|Quackery}}==
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ number$
space 6 of swap join
-7 split nip echo$ ] is rjust ( n --> )
 
say " index prime sum" cr
say " ----- ----- ---" cr
[] 1000 times
[ i^ isprime if [ i^ join ] ]
0 swap
witheach
[ dup dip +
over isprime iff
[ i^ 1+ rjust
rjust
dup rjust
cr ]
else drop ]
drop
</syntaxhighlight>
 
{{out}}
 
<pre> index prime sum
----- ----- ---
1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
my @primesumsprimes = ([\+] = grep *.is-prime, ^Inf)[^1000];
my @primesums = [\+] @primes;
say "Of the first {+@primesums} primes: {.elems} cumulative prime sums:\n",
say "{.elems} cumulative prime sums:\n",
.map( -> $p {
sprintf "The sum of the first %*d3d (up to {@primes[$p]}) is prime: %s",
@primesums.end.chars, 1 + $p, comma @primesums[$p]
}
).join("\n")
given grep { @primesums[$_].is-prime }, ^+@primesums1000;</langsyntaxhighlight>
{{out}}
<pre>Of the first 1000 primes: 76 cumulative prime sums:
The sum of the first 1 (up to 2) is prime: 2
The sum of the first 2 (up to 3) is prime: 5
The sum of the first 4 (up to 7) is prime: 17
The sum of the first 6 (up to 13) is prime: 41
The sum of the first 12 (up to 37) is prime: 197
The sum of the first 14 (up to 43) is prime: 281
The sum of the first 60 (up to 281) is prime: 7,699
The sum of the first 64 (up to 311) is prime: 8,893
The sum of the first 96 (up to 503) is prime: 22,039
The sum of the first 100 (up to 541) is prime: 24,133
The sum of the first 102 (up to 557) is prime: 25,237
The sum of the first 108 (up to 593) is prime: 28,697
The sum of the first 114 (up to 619) is prime: 32,353
The sum of the first 122 (up to 673) is prime: 37,561
The sum of the first 124 (up to 683) is prime: 38,921
The sum of the first 130 (up to 733) is prime: 43,201
The sum of the first 132 (up to 743) is prime: 44,683
The sum of the first 146 (up to 839) is prime: 55,837
The sum of the first 152 (up to 881) is prime: 61,027
The sum of the first 158 (up to 929) is prime: 66,463
The sum of the first 162 (up to 953) is prime: 70,241
The sum of the first 178 (up to 1061) is prime: 86,453
The sum of the first 192 (up to 1163) is prime: 102,001
The sum of the first 198 (up to 1213) is prime: 109,147
The sum of the first 204 (up to 1249) is prime: 116,533
The sum of the first 206 (up to 1277) is prime: 119,069
The sum of the first 208 (up to 1283) is prime: 121,631
The sum of the first 214 (up to 1307) is prime: 129,419
The sum of the first 216 (up to 1321) is prime: 132,059
The sum of the first 296 (up to 1949) is prime: 263,171
The sum of the first 308 (up to 2029) is prime: 287,137
The sum of the first 326 (up to 2161) is prime: 325,019
The sum of the first 328 (up to 2203) is prime: 329,401
The sum of the first 330 (up to 2213) is prime: 333,821
The sum of the first 332 (up to 2237) is prime: 338,279
The sum of the first 334 (up to 2243) is prime: 342,761
The sum of the first 342 (up to 2297) is prime: 360,979
The sum of the first 350 (up to 2357) is prime: 379,667
The sum of the first 356 (up to 2393) is prime: 393,961
The sum of the first 358 (up to 2411) is prime: 398,771
The sum of the first 426 (up to 2957) is prime: 581,921
The sum of the first 446 (up to 3137) is prime: 642,869
The sum of the first 458 (up to 3251) is prime: 681,257
The sum of the first 460 (up to 3257) is prime: 687,767
The sum of the first 464 (up to 3301) is prime: 700,897
The sum of the first 480 (up to 3413) is prime: 754,573
The sum of the first 484 (up to 3461) is prime: 768,373
The sum of the first 488 (up to 3491) is prime: 782,263
The sum of the first 512 (up to 3671) is prime: 868,151
The sum of the first 530 (up to 3821) is prime: 935,507
The sum of the first 536 (up to 3863) is prime: 958,577
The sum of the first 548 (up to 3947) is prime: 1,005,551
The sum of the first 568 (up to 4129) is prime: 1,086,557
The sum of the first 620 (up to 4583) is prime: 1,313,041
The sum of the first 630 (up to 4657) is prime: 1,359,329
The sum of the first 676 (up to 5051) is prime: 1,583,293
The sum of the first 680 (up to 5087) is prime: 1,603,597
The sum of the first 696 (up to 5233) is prime: 1,686,239
The sum of the first 708 (up to 5351) is prime: 1,749,833
The sum of the first 734 (up to 5563) is prime: 1,891,889
The sum of the first 762 (up to 5807) is prime: 2,051,167
The sum of the first 768 (up to 5849) is prime: 2,086,159
The sum of the first 776 (up to 5897) is prime: 2,133,121
The sum of the first 780 (up to 5939) is prime: 2,156,813
The sum of the first 784 (up to 6007) is prime: 2,180,741
The sum of the first 808 (up to 6211) is prime: 2,327,399
The sum of the first 814 (up to 6263) is prime: 2,364,833
The sum of the first 820 (up to 6301) is prime: 2,402,537
The sum of the first 836 (up to 6427) is prime: 2,504,323
The sum of the first 844 (up to 6529) is prime: 2,556,187
The sum of the first 848 (up to 6563) is prime: 2,582,401
The sum of the first 852 (up to 6581) is prime: 2,608,699
The sum of the first 926 (up to 7243) is prime: 3,120,833
The sum of the first 942 (up to 7433) is prime: 3,238,237
The sum of the first 984 (up to 7757) is prime: 3,557,303
The sum of the first 992 (up to 7853) is prime: 3,619,807</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm finds summation primes P, primes which the sum of primes up to P are prime. */
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
call genP /*build array of semaphores for primes.*/
w= 30; w2= w*2%3; pad= left('',w-w2) /*the width of the columns two & three.*/
@sumPtitle= ' summation primes which the sum of primes up to P is also prime, P < ' ,
commas(hi)
say ' index │' center(subword(@sumptitle, 1, 2), w) center('prime sum', w) /*display title.*/
say '───────┼'center("" , 1 + (w+1)*2, '─') /* " sep. */
sPrimesfound= 0 /*initialize # of summation primes. */
pSum= 0 $= 0 /*sum of primes up to the current prime*/
do j=1 for hi-1; p= @.j; pSum$= pSum$ + p /*find summation primes within range. */
if \!.pSum$ then iterate /*Is sum─of─primes a prime? Then skip.*/
sPrimes found= sPrimesfound + 1 /*bump the number of nice summation primes. */
say right(j, 6) '│'strip( right(commas(p), w2)pad || right(commas(pSum$), w2), "T")
end /*j*/
 
say '───────┴'center("" , 1 + (w+1)*2, '─') /*display foot separator after output. */
say
say 'Found ' commas(sPrimesfound) @sumPtitle
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 866 ⟶ 1,646:
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */
#=5; ssq.#= @.# **2 /*number of primes so far; prime². */
/* [↓] generate more primes ≤ high.*/
do j=@.#+2 by 2 until @.#>=hi & @.#>sP /*find odd primes where P≥hi and P>sP.*/
parse var j '' -1 _; if _==5 then iterate /*J divisible÷ by 5? (right digdigit)*/
if j//3==0 then iterate; if j// 37==0 then iterate /*"J ÷ " "by 3?; J ÷ by 7? */
do k=5 while sq.k<=j if j// 7==0 then iterate /*" " " 7? [↓] divide by the known odd primes.*/
/* [↑] the above five lines saves time*/
do k=5 while s.k<=j /* [↓] divide by the known odd primes.*/
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; ssq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P square; P# */
if @.#<hi then sP= sP + @.# /*maybe add this prime to sum─of─primes*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 910 ⟶ 1,688:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 936 ⟶ 1,714:
see "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 965 ⟶ 1,743:
Found 21 numbers
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49g}}
≪ { } 0 0 → display length sum
≪ 1
'''WHILE''' DUP 1000 < '''REPEAT'''
NEXTPRIME
DUP 'sum' STO+
1 'length' STO+
'''IF''' sum ISPRIME? '''THEN'''
length OVER sum 3 →LIST 1 →LIST 'display' SWAP STO+ '''END'''
'''END'''
DROP display
≫ ≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: {{1 2 2} {2 3 5} {3 7 17} {4 13 41} {5 37 197} {6 43 281} {7 281 7699} {8 311 8893} {9 503 22039} {10 541 24133} {11 557 25237} {12 593 28697} {13 619 32353} {14 673 37561} {15 683 38921} {16 733 43201} {17 743 44683} {18 839 55837} {19 881 61027} {20 929 66463} {21 953 70241}}
</pre>
 
=={{header|Ruby}}==
{{trans|C++}}
<langsyntaxhighlight lang="ruby">def isPrime(n)
if n < 2 then
return false
Line 1,013 ⟶ 1,809:
end
end
print "There are %d summerized primes in [%d, %d]\n" % [sc, START, STOP]</langsyntaxhighlight>
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 1,039 ⟶ 1,835:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 1,056 ⟶ 1,852:
}
}
}</langsyntaxhighlight>
 
{{out}}
<pre>
count prime sum
1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241
</pre>
=={{header|Scala}}==
<syntaxhighlight lang="scala">object PrimeSum extends App {
 
val oddPrimes: LazyList[Int] = 3 #:: LazyList.from(5, 2)
.filter(p => oddPrimes.takeWhile(_ <= math.sqrt(p)).forall(p % _ > 0))
val primes = 2 #:: oddPrimes
def isPrime(n: Int): Boolean = {
if (n < 5) (n | 1) == 3
else primes.takeWhile(_ <= math.sqrt(n)).forall(n % _ > 0)
}
val limit = primes.takeWhile(_ <= 1000).length
 
val number = (1 to limit).filter(index => {
val list = primes.take(index)
val sum = list.sum
val flag = isPrime(sum)
if (flag) println(f"$index%3d ${list.last}%3d $sum%5d")
flag
}).length
 
println(s"\nfound $number such primes")
}</syntaxhighlight>
{{out}}
<pre> 1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241
 
found 21 such primes
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">1000.primes.map_reduce {|a,b| a + b }.map_kv {|k,v|
[k+1, prime(k+1), v]
}.grep { .tail.is_prime }.prepend(
['count', 'prime', 'sum']
).each_2d {|n,p,s|
printf("%5s %6s %8s\n", n, p, s)
}</syntaxhighlight>
{{out}}
<pre>
Line 1,087 ⟶ 1,966:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var primes = Int.primeSieve(999)
Line 1,104 ⟶ 1,983:
}
}
System.print("\n%(c) such prime sums found")</langsyntaxhighlight>
 
{{out}}
Line 1,133 ⟶ 2,012:
 
21 such prime sums found
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];
 
int Count, N, Sum, Prime;
[Text(0, "Prime Prime
count sum
");
Count:= 0; N:= 0; Sum:= 0;
for Prime:= 2 to 1000-1 do
if IsPrime(Prime) then
[N:= N+1;
Sum:= Sum + Prime;
if IsPrime(Sum) then
[Count:= Count+1;
IntOut(0, N);
ChOut(0, 9\tab\);
IntOut(0, Sum);
CrLf(0);
];
];
IntOut(0, Count);
Text(0, " prime sums of primes found below 1000.
");
]</syntaxhighlight>
 
{{out}}
<pre>
Prime Prime
count sum
1 2
2 5
4 17
6 41
12 197
14 281
60 7699
64 8893
96 22039
100 24133
102 25237
108 28697
114 32353
122 37561
124 38921
130 43201
132 44683
146 55837
152 61027
158 66463
162 70241
21 prime sums of primes found below 1000.
</pre>
871

edits