Summarize primes

From Rosetta Code
Revision as of 18:42, 15 April 2021 by Hout (talk | contribs) (→‎{{header|Python}}: Added a solution in Python)
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.
Task

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

Factor

Works with: Factor version 0.99 2021-02-05

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

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

Go

Translation of: Wren
Library: Go-rcu

<lang go>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")

}</lang>

Output:
Same as Wren example.


Haskell

<lang haskell>import Data.List (mapAccumL) import Data.Numbers.Primes (isPrime, primes)


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

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

 let ps = primes
  in filter (\(_, _, n) -> isPrime n) $
       snd $
         mapAccumL
           (\a (i, p) -> let m = p + a in (m, (i, p, m)))
           0
           $ zip [1 ..] ps

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

main :: IO () main =

 mapM_ print $
   takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums</lang>
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

<lang 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.")
   end

end

</lang>

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.

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

<lang python>Prime sums of primes up to 1000


from itertools import accumulate, chain, takewhile


  1. 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 filter(
       lambda t: isPrime(t[1][1]),
       enumerate(
           accumulate(
               chain([(0, 0)], primes()),
               lambda a, p: (p, a[1] + p)
           )
       )
   )


  1. ------------------------- TEST -------------------------
  2. 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]}')


  1. ----------------------- GENERIC ------------------------
  1. 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)))


  1. 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


  1. MAIN ---

if __name__ == '__main__':

   main()

</lang>

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

<lang perl6>use Lingua::EN::Numbers;

my @primesums = ([\+] grep *.is-prime, ^Inf)[^1000]; say "Of the first {+@primesums} 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;</lang>
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

Ring

<lang ring> 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 </lang>

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

Wren

Library: Wren-math
Library: Wren-fmt

<lang ecmascript>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")</lang>

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