Find prime n such that reversed n is also prime: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
m (→‎{{header|REXX}}: added a stub.)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 24:
 
=={{header|REXX}}==
<lang rexx>/*REXX program counts/displays the number of reversed primes under a specified number N.*/
<lang rexx></lang>
parse arg n cols . /*get optional number of primes to find*/
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " */
call genP copies(9, length(n) ) /*generate all primes under N. */
w= 10 /*width of a number in any column. */
if cols>0 then say ' index │'center(" reversed primes that are < " n, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
Rprimes= 0; idx= 1 /*initialize # of additive primes & idx*/
$= /*a list of additive primes (so far). */
do j=2 until j>=n; if \!.j then iterate /*Is J not a prime? No, then skip it.*/
_= reverse(j); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/
Rprimes= Rprimes + 1 /*bump the count of additive primes. */
if cols<1 then iterate /*Build the list (to be shown later)? */
$= $ right( commas(j), w) /*add the additive prime to the $ list.*/
if Rprimes//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
 
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say
say 'found ' commas(Rprimes) " reversed primes < " commas(n)
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: parse arg h; @.=.; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; #= 7
w= length(h); !.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1
do j=@.7+2 by 2 while j<h /*continue on with the next odd prime. */
parse var j '' -1 _ /*obtain the last digit of the J var.*/
if _ ==5 then iterate /*is this integer a multiple of five? */
if j // 3 ==0 then iterate /* " " " " " " three? */
/* [↓] divide by the primes. ___ */
do k=4 to # while k*k<=j /*divide J by other primes ≤ √ J */
if j//@.k == 0 then iterate j /*÷ by prev. prime? ¬prime ___ */
end /*k*/ /* [↑] only divide up to √ J */
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/
end /*j*/
return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index │ reversed primes that are < 500
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 2 3 5 7 11 13 17 31 37 71
11 │ 73 79 97 101 107 113 131 149 151 157
21 │ 167 179 181 191 199 311 313 337 347 353
31 │ 359 373 383 389
 
found 34 reversed primes < 500
</pre>
 
{{out|output|text=&nbsp; when using the inputs: &nbsp; &nbsp; <tt> 10000 &nbsp; 0 </tt> }}
<pre>
found 260 reversed primes < 10,000
</pre>
 
=={{header|Ring}}==