Concatenate two primes is also prime: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: expanded output title, added/changed comments and whitespace.) |
|||
Line 481: | Line 481: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX pgm finds |
<lang rexx>/*REXX pgm finds base ten neighbor primes P1 & P2, when concatenated, is also a prime.*/ |
||
parse arg hip cols . /*obtain optional arguments from the CL*/ |
parse arg hip cols . /*obtain optional arguments from the CL*/ |
||
if hip=='' | hip=="," then hip= 100 /*Not specified? Then use the default.*/ |
if hip=='' | hip=="," then hip= 100 /*Not specified? Then use the default.*/ |
||
if cols=='' | cols=="," then cols= 10 /* " " " " " " */ |
if cols=='' | cols=="," then cols= 10 /* " " " " " " */ |
||
call genP /*build array of semaphores for primes.*/ |
call genP /*build array of semaphores for primes.*/ |
||
title= ' concatenation of two primes (P1, P2) in base ten that results in |
title= ' concatenation of two neighbor 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. */ |
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 ' index │'center(title, 1 + cols*(w+1) ) |
||
Line 494: | Line 494: | ||
do j=1 for ## /*search through specified primes. */ |
do j=1 for ## /*search through specified primes. */ |
||
do k=1 for ##; cat2= @.j || @.k /*create a concatenated prime (base 10)*/ |
do k=1 for ##; cat2= @.j || @.k /*create a concatenated prime (base 10)*/ |
||
if !.cat2 then a.cat2= 1 /*flag it as being a "2cat" prime. */ |
if !.cat2 then a.cat2= 1 /*flag it as being a "2cat" prime. */ |
||
end /*k*/ /* [↑] forgoes the need for sorting. */ |
end /*k*/ /* [↑] forgoes the need for sorting. */ |
||
end /*j*/ |
end /*j*/ |
||
found= 0; |
found= 0; idx= 1 /*initialize # of primes found; IDX. */ |
||
$=; do n=1 by 2 for hip**2%2 /*search for odd "cat2" primes. */ |
$=; do n=1 by 2 for hip**2%2 /*search for odd "cat2" primes. */ |
||
if \a.n then iterate /*search through allowable primes. */ |
if \a.n then iterate /*search through allowable primes. */ |
||
Line 512: | Line 512: | ||
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') |
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') |
||
say |
say |
||
say 'Found ' |
say 'Found ' commas(found) title |
||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 521: | Line 521: | ||
#= 5; sq.#= @.#**2 /*the square of the highest low prime. */ |
#= 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. */ |
do j=@.#+2 by 2 to hip**2-1 /*find odd primes from here on. */ |
||
parse var j '' -1 _; |
parse var j '' -1 _; if _==5 then iterate /*J ÷ by 5? (right digit).*/ |
||
if j//3==0 then iterate; if j//7==0 then iterate /*" " " 3? J ÷ by 7? */ |
|||
if j// 7==0 then iterate /*" " " 7? */ |
|||
do k=5 while sq.k<=j /* [↓] divide by the known odd primes.*/ |
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. ___ */ |
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ |
||
Line 532: | Line 531: | ||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
index │ |
index │ concatenation of two neighbor 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 |
1 │ 23 37 53 73 113 137 173 193 197 211 |
||
Line 549: | Line 548: | ||
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
||
Found 128 concatenation of two primes (P1, P2) in base ten that results in a prime, |
Found 128 concatenation of two neighbor primes (P1, P2) in base ten that results in a prime, where P1 & P2 are < 100 |
||
</pre> |
</pre> |
||