Summarize primes: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Factor)
(→‎{{header|Raku}}: Add a Raku example)
Line 37: Line 37:
The sum of the first 162 primes is 70241 (which is prime).
The sum of the first 162 primes is 70241 (which is prime).
</pre>
</pre>

=={{header|Raku}}==
<lang perl6>my @primesums = ([\+] grep *.is-prime, ^Inf)[^1000];
say "Of the first {+@primesums} primes:";
printf "The sum of the first %*d is prime: %d\n", @primesums.end.chars, $_, @primesums[$_]
for grep { @primesums[$_].is-prime }, ^+@primesums;</lang>
{{out}}
<pre>Of the first 1000 primes:
The sum of the first 0 is prime: 2
The sum of the first 1 is prime: 5
The sum of the first 3 is prime: 17
The sum of the first 5 is prime: 41
The sum of the first 11 is prime: 197
The sum of the first 13 is prime: 281
The sum of the first 59 is prime: 7699
The sum of the first 63 is prime: 8893
The sum of the first 95 is prime: 22039
The sum of the first 99 is prime: 24133
The sum of the first 101 is prime: 25237
The sum of the first 107 is prime: 28697
The sum of the first 113 is prime: 32353
The sum of the first 121 is prime: 37561
The sum of the first 123 is prime: 38921
The sum of the first 129 is prime: 43201
The sum of the first 131 is prime: 44683
The sum of the first 145 is prime: 55837
The sum of the first 151 is prime: 61027
The sum of the first 157 is prime: 66463
The sum of the first 161 is prime: 70241
The sum of the first 177 is prime: 86453
The sum of the first 191 is prime: 102001
The sum of the first 197 is prime: 109147
The sum of the first 203 is prime: 116533
The sum of the first 205 is prime: 119069
The sum of the first 207 is prime: 121631
The sum of the first 213 is prime: 129419
The sum of the first 215 is prime: 132059
The sum of the first 295 is prime: 263171
The sum of the first 307 is prime: 287137
The sum of the first 325 is prime: 325019
The sum of the first 327 is prime: 329401
The sum of the first 329 is prime: 333821
The sum of the first 331 is prime: 338279
The sum of the first 333 is prime: 342761
The sum of the first 341 is prime: 360979
The sum of the first 349 is prime: 379667
The sum of the first 355 is prime: 393961
The sum of the first 357 is prime: 398771
The sum of the first 425 is prime: 581921
The sum of the first 445 is prime: 642869
The sum of the first 457 is prime: 681257
The sum of the first 459 is prime: 687767
The sum of the first 463 is prime: 700897
The sum of the first 479 is prime: 754573
The sum of the first 483 is prime: 768373
The sum of the first 487 is prime: 782263
The sum of the first 511 is prime: 868151
The sum of the first 529 is prime: 935507
The sum of the first 535 is prime: 958577
The sum of the first 547 is prime: 1005551
The sum of the first 567 is prime: 1086557
The sum of the first 619 is prime: 1313041
The sum of the first 629 is prime: 1359329
The sum of the first 675 is prime: 1583293
The sum of the first 679 is prime: 1603597
The sum of the first 695 is prime: 1686239
The sum of the first 707 is prime: 1749833
The sum of the first 733 is prime: 1891889
The sum of the first 761 is prime: 2051167
The sum of the first 767 is prime: 2086159
The sum of the first 775 is prime: 2133121
The sum of the first 779 is prime: 2156813
The sum of the first 783 is prime: 2180741
The sum of the first 807 is prime: 2327399
The sum of the first 813 is prime: 2364833
The sum of the first 819 is prime: 2402537
The sum of the first 835 is prime: 2504323
The sum of the first 843 is prime: 2556187
The sum of the first 847 is prime: 2582401
The sum of the first 851 is prime: 2608699
The sum of the first 925 is prime: 3120833
The sum of the first 941 is prime: 3238237
The sum of the first 983 is prime: 3557303
The sum of the first 991 is prime: 3619807</pre>



=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 15:39, 15 April 2021

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

Raku

<lang perl6>my @primesums = ([\+] grep *.is-prime, ^Inf)[^1000]; say "Of the first {+@primesums} primes:"; printf "The sum of the first %*d is prime: %d\n", @primesums.end.chars, $_, @primesums[$_]

   for grep { @primesums[$_].is-prime }, ^+@primesums;</lang>
Output:
Of the first 1000 primes:
The sum of the first   0 is prime: 2
The sum of the first   1 is prime: 5
The sum of the first   3 is prime: 17
The sum of the first   5 is prime: 41
The sum of the first  11 is prime: 197
The sum of the first  13 is prime: 281
The sum of the first  59 is prime: 7699
The sum of the first  63 is prime: 8893
The sum of the first  95 is prime: 22039
The sum of the first  99 is prime: 24133
The sum of the first 101 is prime: 25237
The sum of the first 107 is prime: 28697
The sum of the first 113 is prime: 32353
The sum of the first 121 is prime: 37561
The sum of the first 123 is prime: 38921
The sum of the first 129 is prime: 43201
The sum of the first 131 is prime: 44683
The sum of the first 145 is prime: 55837
The sum of the first 151 is prime: 61027
The sum of the first 157 is prime: 66463
The sum of the first 161 is prime: 70241
The sum of the first 177 is prime: 86453
The sum of the first 191 is prime: 102001
The sum of the first 197 is prime: 109147
The sum of the first 203 is prime: 116533
The sum of the first 205 is prime: 119069
The sum of the first 207 is prime: 121631
The sum of the first 213 is prime: 129419
The sum of the first 215 is prime: 132059
The sum of the first 295 is prime: 263171
The sum of the first 307 is prime: 287137
The sum of the first 325 is prime: 325019
The sum of the first 327 is prime: 329401
The sum of the first 329 is prime: 333821
The sum of the first 331 is prime: 338279
The sum of the first 333 is prime: 342761
The sum of the first 341 is prime: 360979
The sum of the first 349 is prime: 379667
The sum of the first 355 is prime: 393961
The sum of the first 357 is prime: 398771
The sum of the first 425 is prime: 581921
The sum of the first 445 is prime: 642869
The sum of the first 457 is prime: 681257
The sum of the first 459 is prime: 687767
The sum of the first 463 is prime: 700897
The sum of the first 479 is prime: 754573
The sum of the first 483 is prime: 768373
The sum of the first 487 is prime: 782263
The sum of the first 511 is prime: 868151
The sum of the first 529 is prime: 935507
The sum of the first 535 is prime: 958577
The sum of the first 547 is prime: 1005551
The sum of the first 567 is prime: 1086557
The sum of the first 619 is prime: 1313041
The sum of the first 629 is prime: 1359329
The sum of the first 675 is prime: 1583293
The sum of the first 679 is prime: 1603597
The sum of the first 695 is prime: 1686239
The sum of the first 707 is prime: 1749833
The sum of the first 733 is prime: 1891889
The sum of the first 761 is prime: 2051167
The sum of the first 767 is prime: 2086159
The sum of the first 775 is prime: 2133121
The sum of the first 779 is prime: 2156813
The sum of the first 783 is prime: 2180741
The sum of the first 807 is prime: 2327399
The sum of the first 813 is prime: 2364833
The sum of the first 819 is prime: 2402537
The sum of the first 835 is prime: 2504323
The sum of the first 843 is prime: 2556187
The sum of the first 847 is prime: 2582401
The sum of the first 851 is prime: 2608699
The sum of the first 925 is prime: 3120833
The sum of the first 941 is prime: 3238237
The sum of the first 983 is prime: 3557303
The sum of the first 991 is prime: 3619807


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