Summarize primes: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 349:
The sum of the first 984 is prime: 3,557,303
The sum of the first 992 is prime: 3,619,807</pre>
 
=={{header|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>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
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
</pre>
 
=={{header|Ring}}==