Summarize primes: Difference between revisions

New post.
m (added whitespace and a comma.)
(New post.)
 
(32 intermediate revisions by 17 users not shown)
Line 1:
{{Draft taskTask}}
 
;Task:
 
Summarize first   '''n'''   primes   ('''p''')   and check if it is prime,   where  '''p  <  1,000'''.
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}}
<lang algol68>BEGIN # sum the primes below n and report the sums that are prime #
<syntaxhighlight INT max prime lang= 999;"algol68">BEGIN # largest prime to consider sum the primes below n and report the sums that are prime #
# sieve the primes to max prime999 #
PR read "primes.incl.a68" PR
[ 1 : max prime ]BOOL prime;
prime[ 1 ] := FALSE;BOOL prime[ 2= ] :=PRIMESIEVE TRUE999;
FOR i FROM 3 BY 2 TO UPB prime DO prime[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO UPB prime DO prime[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( max prime ) DO
IF prime[ i ] THEN FOR s FROM i * i BY i + i TO UPB prime DO prime[ s ] := FALSE OD FI
OD;
# 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 maxUPB prime DO
IF prime[ i ] THEN
# have another prime #
Line 36 ⟶ 84:
prime sum count +:= 1;
print( ( whole( prime count, -5 )
, " "
, whole( i, -6 )
, " "
, whole( prime sum, -6 )
Line 48 ⟶ 98:
, whole( prime sum count, 0 )
, " prime sums of primes below "
, whole( maxUPB prime + 1, 0 )
, newline
)
)
END</langsyntaxhighlight>
{{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
Line 83 ⟶ 133:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % sum the primes below n and report the sums that are prime %
integer MAX_NUMBER;
MAX_NUMBER := 999;
Line 138 ⟶ 188:
)
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 170 ⟶ 220:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print (pad "index" 6) ++ " | " ++
(pad "prime" 6) ++ " | " ++
(pad "prime sum" 11)
Line 186 ⟶ 236:
(pad to :string s 11)
]
]</langsyntaxhighlight>
 
{{out}}
Line 215 ⟶ 265:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUMMARIZE_PRIMES.AWK
BEGIN {
Line 244 ⟶ 294:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 270 ⟶ 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 323 ⟶ 571:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 347 ⟶ 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 382 ⟶ 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 414 ⟶ 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 445 ⟶ 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 483 ⟶ 812:
 
main
bye</langsyntaxhighlight>
 
{{out}}
<pre>count prime sum
<pre>
count prime sum
1 2 2
2 3 5
Line 508 ⟶ 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 574 ⟶ 873:
fmt.Println()
fmt.Println(c, "such prime sums found")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 581 ⟶ 880:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (scanl)
import Data.Numbers.Primes (isPrime, primes)
 
Line 599 ⟶ 898:
mapM_ print $
takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums
</syntaxhighlight>
</lang>
{{Out}}
<pre>(1,2,2)
Line 622 ⟶ 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 635 ⟶ 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 659 ⟶ 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 683 ⟶ 1,195:
inc idx
s += n
if s.isPrime: echo &"{idx:3} {n:5} {s:7}"</langsyntaxhighlight>
 
{{out}}
Line 711 ⟶ 1,223:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <nth_prime is_prime>;
Line 720 ⟶ 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 802 ⟶ 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 887 ⟶ 1,453:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>1 -> 2
Line 910 ⟶ 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 @primes = grep *.is-prime, ^Inf;
Line 922 ⟶ 1,538:
}
).join("\n")
given grep { @primesums[$_].is-prime }, ^1000;</langsyntaxhighlight>
{{out}}
<pre>76 cumulative prime sums:
Line 1,003 ⟶ 1,619:
 
=={{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 . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 1,030 ⟶ 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.*/
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 1,073 ⟶ 1,688:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 1,099 ⟶ 1,714:
see "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,128 ⟶ 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,176 ⟶ 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,202 ⟶ 1,835:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 1,219 ⟶ 1,852:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,245 ⟶ 1,878:
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}}==
<langsyntaxhighlight 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(
Line 1,254 ⟶ 1,936:
).each_2d {|n,p,s|
printf("%5s %6s %8s\n", n, p, s)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,284 ⟶ 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,301 ⟶ 1,983:
}
}
System.print("\n%(c) such prime sums found")</langsyntaxhighlight>
 
{{out}}
Line 1,333 ⟶ 2,015:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,361 ⟶ 2,043:
Text(0, " prime sums of primes found below 1000.
");
]</langsyntaxhighlight>
 
{{out}}
894

edits