Summarize primes: Difference between revisions

Content added Content deleted
(added AWK)
(Added Perl)
Line 372: Line 372:
The sum of the 162 primes from prime 2 to prime 953 is 70241, which is prime.
The sum of the 162 primes from prime 2 to prime 953 is 70241, which is prime.
</pre>
</pre>

=={{header|Perl}}==
{{libheader|ntheory}}
<lang perl>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;</lang>
{{out}}
<pre style="height:70ex">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</pre>


=={{header|Phix}}==
=={{header|Phix}}==