Concatenate two primes is also prime: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
(add verbiage to the task's requirements, added requirement that the primes be shown in base ten, add requirement that results should be shown here, added PRIME NUMBERS category..)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 161:
6779 7129 7159 7331 7919 7937 8311 8317 8329 8353
8389 8923 8929 8941 8971 9719 9743 9767</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds & displays base ten primes P1 & P2, when concatenated, is also a prime.*/
parse arg hip cols . /*obtain optional arguments from the CL*/
if hip=='' | hip=="," then hip= 100 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
call genP /*build array of semaphores for primes.*/
title= ' concatenation of two primes (P1, P2) in base ten that results in a prime, ' ,
" where P1 & P2 are < " commas(hip)
w= 10 /*width of a number in any column. */
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
a.= 0 /*array of "2cat" primes */
do j=1 for ##; p1= @.j /*search for "cat2" primes. */
do k=1 for ##; p2= @.k /*search through allowable primes. */
cat2= p1 || p2 /*create a concatenated prime (base 10)*/
if !.cat2 then a.cat2= 1 /*flag it as being a "2cat" prime. */
end /*k*/ /* [↑] forgoes the need for sorting. */
end /*j*/
found= 0; idx= 1 /*initialize # of primes found; IDX. */
$=; do n=1 by 2 for hip**2%2 /*search for odd "cat2" primes. */
if \a.n then iterate /*search through allowable primes. */
found= found + 1 /*bump the number of primes found. */
if cols<=0 then iterate /*Build the list (to be shown later)? */
c= commas(n) /*maybe add commas to the number. */
$= $ right(c, max(w, length(c) ) ) /*add a prime ──► $ list, allow big #*/
if found//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 /*n*/
 
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' commas(found) title
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 /*the square of the highest low prime. */
do j=@.#+2 by 2 to hip**2-1 /*find odd primes from here on. */
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? */
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# */
if j<hip then ##= # /*find a shortcut for the 1st DO loop. */
end /*j*/; return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index │ concatenation of two primes (P1, P2) in base ten that results in a prime, where P1 & P2 are < 100
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 23 37 53 73 113 137 173 193 197 211
11 │ 223 229 233 241 271 283 293 311 313 317
21 │ 331 337 347 353 359 367 373 379 383 389
31 │ 397 433 523 541 547 571 593 613 617 673
41 │ 677 719 733 743 761 773 797 977 1,117 1,123
51 │ 1,129 1,153 1,171 1,319 1,361 1,367 1,373 1,723 1,741 1,747
61 │ 1,753 1,759 1,783 1,789 1,913 1,931 1,973 1,979 1,997 2,311
71 │ 2,341 2,347 2,371 2,383 2,389 2,917 2,953 2,971 3,119 3,137
81 │ 3,167 3,719 3,761 3,767 3,779 3,797 4,111 4,129 4,153 4,159
91 │ 4,337 4,373 4,397 4,723 4,729 4,759 4,783 4,789 5,323 5,347
101 │ 5,923 5,953 6,113 6,131 6,143 6,173 6,197 6,719 6,737 6,761
111 │ 6,779 7,129 7,159 7,331 7,919 7,937 8,311 8,317 8,329 8,353
121 │ 8,389 8,923 8,929 8,941 8,971 9,719 9,743 9,767
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
 
Found 128 concatenation of two primes (P1, P2) in base ten that results in a prime, where P1 & P2 are < 100
</pre>
 
=={{header|Ring}}==