# Summarize primes

**Summarize primes**

You are encouraged to solve this task according to the task description, using any language you may know.

- Task

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.

## Contents

## 11l[edit]

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}’)

- Output:

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

## ALGOL 68[edit]

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

- Output:

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

## ALGOL W[edit]

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.

- Output:

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

## Arturo[edit]

print (pad "index" 6) ++ " | " ++

(pad "prime" 6) ++ " | " ++

(pad "prime sum" 11)

print "------------------------------"

s: 0

idx: 0

loop 2..999 'n [

if prime? n [

idx: idx + 1

s: s + n

if prime? s ->

print (pad to :string idx 6) ++ " | " ++

(pad to :string n 6) ++ " | " ++

(pad to :string s 11)

]

]

- Output:

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

## AWK[edit]

# syntax: GAWK -f SUMMARIZE_PRIMES.AWK

BEGIN {

start = 1

stop = 999

for (i=start; i<=stop; i++) {

if (is_prime(i)) {

count1++

sum += i

if (is_prime(sum)) {

printf("the sum of %3d primes from primes 2-%-3s is %5d which is also prime\n",count1,i,sum)

count2++

}

}

}

printf("Summarized primes %d-%d: %d\n",start,stop,count2)

exit(0)

}

function is_prime(x, i) {

if (x <= 1) {

return(0)

}

for (i=2; i<=int(sqrt(x)); i++) {

if (x % i == 0) {

return(0)

}

}

return(1)

}

- Output:

the sum of 1 primes from primes 2-2 is 2 which is also prime the sum of 2 primes from primes 2-3 is 5 which is also prime the sum of 4 primes from primes 2-7 is 17 which is also prime the sum of 6 primes from primes 2-13 is 41 which is also prime the sum of 12 primes from primes 2-37 is 197 which is also prime the sum of 14 primes from primes 2-43 is 281 which is also prime the sum of 60 primes from primes 2-281 is 7699 which is also prime the sum of 64 primes from primes 2-311 is 8893 which is also prime the sum of 96 primes from primes 2-503 is 22039 which is also prime the sum of 100 primes from primes 2-541 is 24133 which is also prime the sum of 102 primes from primes 2-557 is 25237 which is also prime the sum of 108 primes from primes 2-593 is 28697 which is also prime the sum of 114 primes from primes 2-619 is 32353 which is also prime the sum of 122 primes from primes 2-673 is 37561 which is also prime the sum of 124 primes from primes 2-683 is 38921 which is also prime the sum of 130 primes from primes 2-733 is 43201 which is also prime the sum of 132 primes from primes 2-743 is 44683 which is also prime the sum of 146 primes from primes 2-839 is 55837 which is also prime the sum of 152 primes from primes 2-881 is 61027 which is also prime the sum of 158 primes from primes 2-929 is 66463 which is also prime the sum of 162 primes from primes 2-953 is 70241 which is also prime Summarized primes 1-999: 21

## C[edit]

#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;

}

- Output:

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)

## C++[edit]

#include <iostream>

bool is_prime(int n) {

if (n < 2) {

return false;

}

if (n % 2 == 0) {

return n == 2;

}

if (n % 3 == 0) {

return n == 3;

}

int i = 5;

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;

for (int 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;

}

- Output:

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)

## F#[edit]

This task uses Extensible Prime Generator (F#)

// 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)

- Output:

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 Real: 00:00:00.015

## Factor[edit]

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

- Output:

The sum of the first 1 primes is 2 (which is prime). The sum of the first 2 primes is 5 (which is prime). The sum of the first 4 primes is 17 (which is prime). The sum of the first 6 primes is 41 (which is prime). The sum of the first 12 primes is 197 (which is prime). The sum of the first 14 primes is 281 (which is prime). The sum of the first 60 primes is 7699 (which is prime). The sum of the first 64 primes is 8893 (which is prime). The sum of the first 96 primes is 22039 (which is prime). The sum of the first 100 primes is 24133 (which is prime). The sum of the first 102 primes is 25237 (which is prime). The sum of the first 108 primes is 28697 (which is prime). The sum of the first 114 primes is 32353 (which is prime). The sum of the first 122 primes is 37561 (which is prime). The sum of the first 124 primes is 38921 (which is prime). The sum of the first 130 primes is 43201 (which is prime). The sum of the first 132 primes is 44683 (which is prime). The sum of the first 146 primes is 55837 (which is prime). The sum of the first 152 primes is 61027 (which is prime). The sum of the first 158 primes is 66463 (which is prime). The sum of the first 162 primes is 70241 (which is prime).

## Fermat[edit]

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

- Output:

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

## Forth[edit]

: prime? ( n -- flag )

dup 2 < if drop false exit then

dup 2 mod 0= if 2 = exit then

dup 3 mod 0= if 3 = exit then

5

begin

2dup dup * >=

while

2dup mod 0= if 2drop false exit then

2 +

2dup mod 0= if 2drop false exit then

4 +

repeat

2drop true ;

: main

0 0 { count sum }

." count prime sum" cr

1000 2 do

i prime? if

count 1+ to count

sum i + to sum

sum prime? if

." " count 3 .r ." " i 3 .r ." " sum 5 .r cr

then

then

loop ;

main

bye

- Output:

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

## FreeBASIC[edit]

#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

- Output:

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

## Fōrmulæ[edit]

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.

Programs in Fōrmulæ are created/edited online in its website, However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.

In **this** page you can see the program(s) related to this task and their results.

## Go[edit]

package main

import (

"fmt"

"rcu"

)

func main() {

primes := rcu.Primes(999)

sum, n, c := 0, 0, 0

fmt.Println("Summing the first n primes (<1,000) where the sum is itself prime:")

fmt.Println(" n cumulative sum")

for _, p := range primes {

n++

sum += p

if rcu.IsPrime(sum) {

c++

fmt.Printf("%3d %6s\n", n, rcu.Commatize(sum))

}

}

fmt.Println()

fmt.Println(c, "such prime sums found")

}

- Output:

Same as Wren example.

## Haskell[edit]

import Data.List (scanl)

import Data.Numbers.Primes (isPrime, primes)

--------------- PRIME SUMS OF FIRST N PRIMES -------------

indexedPrimeSums :: [(Integer, Integer, Integer)]

indexedPrimeSums =

filter (\(_, _, n) -> isPrime n) $

scanl

(\(i, _, m) p -> (succ i, p, p + m))

(0, 0, 0)

primes

--------------------------- TEST -------------------------

main :: IO ()

main =

mapM_ print $

takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums

- Output:

(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)

## jq[edit]

**Works with gojq, the Go implementation of 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)." )

- Output:

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.

## Julia[edit]

using Primes

p1000 = primes(1000)

for n in 1:length(p1000)

parray = p1000[1:n]

sparray = sum(parray)

if isprime(sparray)

println("The sum of the $n primes from prime 2 to prime $(p1000[n]) is $sparray, which is prime.")

end

end

- Output:

The sum of the 1 primes from prime 2 to prime 2 is 2, which is prime. The sum of the 2 primes from prime 2 to prime 3 is 5, which is prime. The sum of the 4 primes from prime 2 to prime 7 is 17, which is prime. The sum of the 6 primes from prime 2 to prime 13 is 41, which is prime. The sum of the 12 primes from prime 2 to prime 37 is 197, which is prime. The sum of the 14 primes from prime 2 to prime 43 is 281, which is prime. The sum of the 60 primes from prime 2 to prime 281 is 7699, which is prime. The sum of the 64 primes from prime 2 to prime 311 is 8893, which is prime. The sum of the 96 primes from prime 2 to prime 503 is 22039, which is prime. The sum of the 100 primes from prime 2 to prime 541 is 24133, which is prime. The sum of the 102 primes from prime 2 to prime 557 is 25237, which is prime. The sum of the 108 primes from prime 2 to prime 593 is 28697, which is prime. The sum of the 114 primes from prime 2 to prime 619 is 32353, which is prime. The sum of the 122 primes from prime 2 to prime 673 is 37561, which is prime. The sum of the 124 primes from prime 2 to prime 683 is 38921, which is prime. The sum of the 130 primes from prime 2 to prime 733 is 43201, which is prime. The sum of the 132 primes from prime 2 to prime 743 is 44683, which is prime. The sum of the 146 primes from prime 2 to prime 839 is 55837, which is prime. The sum of the 152 primes from prime 2 to prime 881 is 61027, which is prime. The sum of the 158 primes from prime 2 to prime 929 is 66463, which is prime. The sum of the 162 primes from prime 2 to prime 953 is 70241, which is prime.

## Mathematica/Wolfram Language[edit]

p = Prime[Range[PrimePi[1000]]];

TableForm[

Select[Transpose[{Range[Length[p]], p, Accumulate[p]}], Last /* PrimeQ],

TableHeadings -> {None, {"Prime count", "Prime", "Prime sum"}}

]

- Output:

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

## Nim[edit]

import math, strformat

const N = 999

func isPrime(n: Positive): bool =

if (n and 1) == 0: return n == 2

if (n mod 3) == 0: return n == 3

var d = 5

var delta = 2

while d <= sqrt(n.toFloat).int:

if n mod d == 0: return false

inc d, delta

delta = 6 - delta

result = true

echo "index prime prime sum"

var s = 0

var idx = 0

for n in 2..N:

if n.isPrime:

inc idx

s += n

if s.isPrime: echo &"{idx:3} {n:5} {s:7}"

- Output:

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

## Perl[edit]

use strict;

use warnings;

use ntheory <nth_prime is_prime>;

my($n, $s, $limit, @sums) = (0, 0, 1000);

do {

push @sums, sprintf '%3d %8d', $n, $s if is_prime($s += nth_prime ++$n)

} until $n >= $limit;

print "Of the first $limit primes: @{[scalar @sums]} cumulative prime sums:\n", join "\n", @sums;

- Output:

Of the first 1000 primes: 76 cumulative prime sums: 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 178 86453 192 102001 198 109147 204 116533 206 119069 208 121631 214 129419 216 132059 296 263171 308 287137 326 325019 328 329401 330 333821 332 338279 334 342761 342 360979 350 379667 350 379667 356 393961 358 398771 426 581921 446 642869 458 681257 460 687767 464 700897 480 754573 484 768373 488 782263 512 868151 530 935507 536 958577 548 1005551 568 1086557 620 1313041 630 1359329 676 1583293 680 1603597 696 1686239 708 1749833 734 1891889 762 2051167 768 2086159 776 2133121 780 2156813 784 2180741 808 2327399 814 2364833 820 2402537 836 2504323 844 2556187 848 2582401 852 2608699 926 3120833 942 3238237 984 3557303 992 3619807

## Phix[edit]

function sp(integer n) return is_prime(sum(get_primes(-n))) end function sequence res = apply(filter(tagset(length(get_primes_le(1000))),sp),sprint) printf(1,"Found %d of em: %s\n",{length(res),join(shorten(res,"",5),", ")})

- Output:

Found 21 of em: 1, 2, 4, 6, 12, ..., 132, 146, 152, 158, 162

## Python[edit]

'''Prime sums of primes up to 1000'''

from itertools import accumulate, chain, takewhile

# primeSums :: [(Int, (Int, Int))]

def primeSums():

'''Non finite stream of enumerated tuples,

in which the first value is a prime,

and the second the sum of that prime and all

preceding primes.

'''

return (

x for x in enumerate(

accumulate(

chain([(0, 0)], primes()),

lambda a, p: (p, p + a[1])

)

) if isPrime(x[1][1])

)

# ------------------------- TEST -------------------------

# main :: IO ()

def main():

'''Prime sums of primes below 1000'''

for x in takewhile(

lambda t: 1000 > t[1][0],

primeSums()

):

print(f'{x[0]} -> {x[1][1]}')

# ----------------------- GENERIC ------------------------

# isPrime :: Int -> Bool

def isPrime(n):

'''True if n is prime.'''

if n in (2, 3):

return True

if 2 > n or 0 == n % 2:

return False

if 9 > n:

return True

if 0 == n % 3:

return False

def p(x):

return 0 == n % x or 0 == n % (2 + x)

return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))

# primes :: [Int]

def primes():

''' Non finite sequence of prime numbers.

'''

n = 2

dct = {}

while True:

if n in dct:

for p in dct[n]:

dct.setdefault(n + p, []).append(p)

del dct[n]

else:

yield n

dct[n * n] = [n]

n = 1 + n

# MAIN ---

if __name__ == '__main__':

main()

- Output:

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

## Raku[edit]

use Lingua::EN::Numbers;

my @primes = grep *.is-prime, ^Inf;

my @primesums = [\+] @primes;

say "{.elems} cumulative prime sums:\n",

.map( -> $p {

sprintf "The sum of the first %3d (up to {@primes[$p]}) is prime: %s",

1 + $p, comma @primesums[$p]

}

).join("\n")

given grep { @primesums[$_].is-prime }, ^1000;

- Output:

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

## REXX[edit]

/*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.*/

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.*/

title= ' summation primes which the sum of primes up to P is also prime, P < ' ,

commas(hi)

say ' index │' center(subword(title, 1, 2), w) center('prime sum', w) /*display title.*/

say '───────┼'center("" , 1 + (w+1)*2, '─') /* " sep. */

found= 0 /*initialize # of summation primes. */

$= 0 /*sum of primes up to the current prime*/

do j=1 for hi-1; p= @.j; $= $ + p /*find summation primes within range. */

if \!.$ then iterate /*Is sum─of─primes a prime? Then skip.*/

found= found + 1 /*bump the number of summation primes. */

say right(j, 6) '│'strip( right(commas(p), w2)pad || right(commas($), w2), "T")

end /*j*/

say '───────┴'center("" , 1 + (w+1)*2, '─') /*display foot separator after output. */

say

say 'Found ' commas(found) title

exit 0 /*stick a fork in it, we're all done. */

/*──────────────────────────────────────────────────────────────────────────────────────*/

commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?

/*──────────────────────────────────────────────────────────────────────────────────────*/

genP: !.= 0; sP= 0 /*prime semaphores; sP= sum of primes.*/

@.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; sq.#= @.# **2 /*number of primes so far; prime². */

/* [↓] generate more primes ≤ high.*/

do [email protected].#+2 by 2 until @.#>=hi & @.#>sP /*find odd primes where P≥hi and P>sP.*/

parse var j '' -1 _; if _==5 then iterate /*J ÷ by 5? (right digit)*/

if j//3==0 then iterate; if j//7==0 then iterate /*J ÷ by 3?; J ÷ by 7? */

do k=5 while sq.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; sq.#= j*j; !.j= 1 /*bump # Ps; assign next P; P square; P*/

if @.#<hi then sP= sP + @.# /*maybe add this prime to sum─of─primes*/

end /*j*/; return

- output when using the default inputs:

index │ summation primes prime sum ───────┼─────────────────────────────────────────────────────────────── 1 │ 2 2 2 │ 3 5 4 │ 7 17 6 │ 13 41 12 │ 37 197 14 │ 43 281 60 │ 281 7,699 64 │ 311 8,893 96 │ 503 22,039 100 │ 541 24,133 102 │ 557 25,237 108 │ 593 28,697 114 │ 619 32,353 122 │ 673 37,561 124 │ 683 38,921 130 │ 733 43,201 132 │ 743 44,683 146 │ 839 55,837 152 │ 881 61,027 158 │ 929 66,463 162 │ 953 70,241 ───────┴─────────────────────────────────────────────────────────────── Found 21 summation primes which the sum of primes up to P is also prime, P < 1,000

## Ring[edit]

load "stdlib.ring"

see "working..." + nl

see "Summarize primes:" + nl

see "n sum" + nl

row = 0

sum = 0

limit = 1000

Primes = []

for n = 2 to limit

if isprime(n)

add(Primes,n)

ok

next

for n = 1 to len(Primes)

sum = sum + Primes[n]

if isprime(sum)

row = row + 1

see "" + n + " " + sum + nl

ok

next

see "Found " + row + " numbers" + nl

see "done..." + nl

- Output:

working... Summarize primes: n 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 numbers done...

## Ruby[edit]

def isPrime(n)

if n < 2 then

return false

end

if n % 2 == 0 then

return n == 2

end

if n % 3 == 0 then

return n == 3

end

i = 5

while i * i <= n

if n % i == 0 then

return false

end

i += 2

if n % i == 0 then

return false

end

i += 4

end

return true

end

START = 1

STOP = 1000

sum = 0

count = 0

sc = 0

for p in START .. STOP

if isPrime(p) then

count += 1

sum += p

if isPrime(sum) then

print "The sum of %3d primes in [2, %3d] is %5d which is also prime\n" % [count, p, sum]

sc += 1

end

end

end

print "There are %d summerized primes in [%d, %d]\n" % [sc, START, STOP]

- Output:

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]

## Rust[edit]

// [dependencies]

// primal = "0.3"

fn main() {

let limit = 1000;

let mut sum = 0;

println!("count prime sum");

for (n, p) in primal::Sieve::new(limit)

.primes_from(2)

.take_while(|x| *x < limit)

.enumerate()

{

sum += p;

if primal::is_prime(sum as u64) {

println!(" {:>3} {:>3} {:>5}", n + 1, p, sum);

}

}

}

- Output:

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

## Sidef[edit]

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)

}

- Output:

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

## Wren[edit]

import "/math" for Int

import "/fmt" for Fmt

var primes = Int.primeSieve(999)

var sum = 0

var n = 0

var c = 0

System.print("Summing the first n primes (<1,000) where the sum is itself prime:")

System.print(" n cumulative sum")

for (p in primes) {

n = n + 1

sum = sum + p

if (Int.isPrime(sum)) {

c = c + 1

Fmt.print("$3d $,6d", n, sum)

}

}

System.print("\n%(c) such prime sums found")

- Output:

Summing the first n primes (<1,000) where the sum is itself prime: n cumulative sum 1 2 2 5 4 17 6 41 12 197 14 281 60 7,699 64 8,893 96 22,039 100 24,133 102 25,237 108 28,697 114 32,353 122 37,561 124 38,921 130 43,201 132 44,683 146 55,837 152 61,027 158 66,463 162 70,241 21 such prime sums found

## XPL0[edit]

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.

");

]

- Output:

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.