Summarize primes
- Task
Summarize first n primes (p) and check if it is a prime, where p < 1000
Factor
<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).
Fermat
<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 </lang>
- 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
FreeBASIC
<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</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 66463162 953 70241
Go
<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
- 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, a[1] + p) ) ) 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()
</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
REXX
<lang rexx>/*REXX pgm finds summation primes P, primes which the sum of primes up to P are prime. */ parse arg hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ call genP /*build array of semaphores for primes.*/ w= 30; w2= w*2%3; pad= left(,w-w2) /*the width of the columns two & three.*/ @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. */ say say 'Found ' commas(sPrimes) @sumP 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; s.#= @.# **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 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</lang>
- 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
<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
<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