I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

# Summarize primes

Summarize primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Summarize first n primes (p) and check if it is a prime, where p < 1000

## AWK

` # syntax: GAWK -f SUMMARIZE_PRIMES.AWKBEGIN {    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
```

## F#

This task uses Extensible Prime Generator (F#)

` // Summarize Primes: Nigel Galloway. April 16th., 2021primes32()|>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

Works with: Factor version 0.99 2021-02-05
`USING: assocs formatting kernel math.primes math.rangesmath.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

`n:=0s:=0for 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

Works with: Gforth
`: prime? ( n -- ? ) here + [email protected] 0= ;: notprime! ( n -- ) here + 1 swap c! ; : prime_sieve { n -- }  here n erase  0 notprime!  1 notprime!  n 4 > if    n 4 do i notprime! 2 +loop  then  3  begin    dup dup * n <  while    dup prime? if      n over dup * do        i notprime!      dup 2* +loop    then    2 +  repeat  drop ; : main  0 0 { count sum }  ." count  prime      sum" cr  100000 prime_sieve  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 ; mainbye`
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

`#include "isprime.bas" print 1,2,2dim as integer sum = 2, i, n=1for 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 ifnext 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```

## Go

Translation of: Wren
Library: Go-rcu
`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.
```

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

## Julia

`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.")    endend `
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.
```

## Perl

Library: ntheory
`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

```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

`'''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)            )        ) if isPrime(x)    )  # ------------------------- TEST -------------------------# main :: IO ()def main():    '''Prime sums of primes below 1000'''    for x in takewhile(            lambda t: 1000 > t,            primeSums()    ):        print(f'{x} -> {x}')  # ----------------------- GENERIC ------------------------ # isPrime :: Int -> Booldef 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

`use Lingua::EN::Numbers; my @primesums = ([\+] grep *.is-prime, ^Inf)[^1000];say "Of the first {[email protected]} primes: {.elems} cumulative prime sums:\n",    .map( -> \$p {        sprintf "The sum of the first %*d is prime: %s",        @primesums.end.chars, 1 + \$p, comma @primesums[\$p]      }    ).join("\n")    given grep { @primesums[\$_].is-prime }, ^+@primesums;`
Output:
```Of the first 1000 primes: 76 cumulative prime sums:
The sum of the first   1 is prime: 2
The sum of the first   2 is prime: 5
The sum of the first   4 is prime: 17
The sum of the first   6 is prime: 41
The sum of the first  12 is prime: 197
The sum of the first  14 is prime: 281
The sum of the first  60 is prime: 7,699
The sum of the first  64 is prime: 8,893
The sum of the first  96 is prime: 22,039
The sum of the first 100 is prime: 24,133
The sum of the first 102 is prime: 25,237
The sum of the first 108 is prime: 28,697
The sum of the first 114 is prime: 32,353
The sum of the first 122 is prime: 37,561
The sum of the first 124 is prime: 38,921
The sum of the first 130 is prime: 43,201
The sum of the first 132 is prime: 44,683
The sum of the first 146 is prime: 55,837
The sum of the first 152 is prime: 61,027
The sum of the first 158 is prime: 66,463
The sum of the first 162 is prime: 70,241
The sum of the first 178 is prime: 86,453
The sum of the first 192 is prime: 102,001
The sum of the first 198 is prime: 109,147
The sum of the first 204 is prime: 116,533
The sum of the first 206 is prime: 119,069
The sum of the first 208 is prime: 121,631
The sum of the first 214 is prime: 129,419
The sum of the first 216 is prime: 132,059
The sum of the first 296 is prime: 263,171
The sum of the first 308 is prime: 287,137
The sum of the first 326 is prime: 325,019
The sum of the first 328 is prime: 329,401
The sum of the first 330 is prime: 333,821
The sum of the first 332 is prime: 338,279
The sum of the first 334 is prime: 342,761
The sum of the first 342 is prime: 360,979
The sum of the first 350 is prime: 379,667
The sum of the first 356 is prime: 393,961
The sum of the first 358 is prime: 398,771
The sum of the first 426 is prime: 581,921
The sum of the first 446 is prime: 642,869
The sum of the first 458 is prime: 681,257
The sum of the first 460 is prime: 687,767
The sum of the first 464 is prime: 700,897
The sum of the first 480 is prime: 754,573
The sum of the first 484 is prime: 768,373
The sum of the first 488 is prime: 782,263
The sum of the first 512 is prime: 868,151
The sum of the first 530 is prime: 935,507
The sum of the first 536 is prime: 958,577
The sum of the first 548 is prime: 1,005,551
The sum of the first 568 is prime: 1,086,557
The sum of the first 620 is prime: 1,313,041
The sum of the first 630 is prime: 1,359,329
The sum of the first 676 is prime: 1,583,293
The sum of the first 680 is prime: 1,603,597
The sum of the first 696 is prime: 1,686,239
The sum of the first 708 is prime: 1,749,833
The sum of the first 734 is prime: 1,891,889
The sum of the first 762 is prime: 2,051,167
The sum of the first 768 is prime: 2,086,159
The sum of the first 776 is prime: 2,133,121
The sum of the first 780 is prime: 2,156,813
The sum of the first 784 is prime: 2,180,741
The sum of the first 808 is prime: 2,327,399
The sum of the first 814 is prime: 2,364,833
The sum of the first 820 is prime: 2,402,537
The sum of the first 836 is prime: 2,504,323
The sum of the first 844 is prime: 2,556,187
The sum of the first 848 is prime: 2,582,401
The sum of the first 852 is prime: 2,608,699
The sum of the first 926 is prime: 3,120,833
The sum of the first 942 is prime: 3,238,237
The sum of the first 984 is prime: 3,557,303
The sum of the first 992 is prime: 3,619,807```

## 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.*/@sumP= ' summation primes which the sum of primes up to  P  is also prime,  P  < '  ,                                    commas(hi)say ' index │' center(subword(@sump, 1, 2), w)  center('prime sum', w)  /*display title.*/say '───────┼'center(""   , 1 + (w+1)*2,  '─')                          /*   "    sep.  */sPrimes= 0                                       /*initialize # of summation primes.    */pSum= 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= sPrimes + 1                     /*bump the number of  nice  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. */saysay 'Found '       commas(sPrimes)      @sumPexit 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;     s.#= @.# **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 divisible by 5?  (right dig)*/                             if j// 3==0  then iterate  /*"     "      " 3?             */                             if j// 7==0  then iterate  /*"     "      " 7?             */                                                 /* [↑]  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;    s.#= j*j;   !.j= 1 /*bump # of Ps; assign next P;  P²; 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

` load "stdlib.ring"see "working..." + nlsee "Summarize primes:" + nlsee "n sum" + nlrow = 0sum = 0limit = 1000Primes = [] for n = 2 to limit    if isprime(n)       add(Primes,n)    oknext for n = 1 to len(Primes)    sum = sum + Primes[n]    if isprime(sum)       row = row + 1       see "" + n + " " + sum + nl    oknext see "Found " + row + " numbers" + nlsee "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...
```

## Rust

`// [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
```

## Wren

Library: Wren-math
Library: Wren-fmt
`import "/math" for Intimport "/fmt" for Fmt var primes = Int.primeSieve(999)var sum = 0var n = 0var c = 0System.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
```