Sum of primes in odd positions is prime: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 167:
653 17959
823 26879</pre>
 
=={{header|REXX}}==
<lang REXX>/*REXX pgm shows a prime index, the prime, & sum of odd indexed primes when sum is prime*/
parse arg hi . /*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.*/
title= 'odd indexed primes the sum of the odd indexed primes'
say ' index │'center(title, 65)
say '───────┼'center("" , 65, '─')
found= 0 /*initialize # of odd indexed primes···*/
$= 0 /*sum of odd indexed primes (so far). */
do j=1 by 2; p= @.j; if p>hi then leave /*find odd indexed primes, sum = prime.*/
$= $ + p /*add this odd index prime to the sum. */
if \!.$ then iterate /*This sum not prime? Then skip it. */
found= found + 1 /*bump the number of solutions found. */
say center(j, 7)'│' right( commas(p), 13) right( commas($), 33)
end /*j*/
 
say '───────┴'center("" , 65, '─')
say
say 'Found ' commas(found) ' 'subword(title, 1, 3)
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: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
!.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " semaphores. */
#=5; sq.#= @.# ** 2 /*number of primes so far; prime². */
do j=@.#+2 by 2 to hi*33; parse var j '' -1 _ /*obtain the last decimal dig.*/
if _==5 then iterate; if j//3==0 then iterate; if j//7==0 then iterate
do k=5 while sq.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; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index │ odd indexed primes the sum of the odd indexed primes
───────┼─────────────────────────────────────────────────────────────────
1 │ 2 2
3 │ 5 7
11 │ 31 89
27 │ 103 659
35 │ 149 1,181
67 │ 331 5,021
91 │ 467 9,923
95 │ 499 10,909
99 │ 523 11,941
119 │ 653 17,959
143 │ 823 26,879
───────┴─────────────────────────────────────────────────────────────────
 
Found 11 odd indexed primes
</pre>
 
=={{header|Ring}}==