Substring primes: Difference between revisions
Content added Content deleted
(Added Go) |
m (→{{header|REXX}}: changed comments and some variable names.) |
||
Line 558: | Line 558: | ||
say ' index │'center(title, 1 + cols*(w+1) ) /*display the title of the output. */ |
say ' index │'center(title, 1 + cols*(w+1) ) /*display the title of the output. */ |
||
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " " separator " " " */ |
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " " separator " " " */ |
||
found= 0; |
found= 0; idx= 1 /*define # substring primes found; IDX.*/ |
||
$= /*a list of substring primes (so far). */ |
$= /*a list of substring primes (so far). */ |
||
do j=1 for #; |
do j=1 for #; p= @.j; p2= substr(p, 2) /*search for primes that fit criteria. */ |
||
if verify( |
if verify(p, 014689, 'M')>0 then iterate /*does it contain any of these digits? */ |
||
if verify( |
if verify(p2, 25 , 'M')>0 then iterate /* " P2 " " " " " */ |
||
L= length( |
L= length(p) /*obtain the length of the P prime.*/ |
||
do k=1 for L-1 /*test for primality for all substrings*/ |
do k=1 for L-1 /*test for primality for all substrings*/ |
||
do m=k+1 to L; y= substr( |
do m=k+1 to L; y= substr(p, k, m-1) /*extract a substring from the P prime.*/ |
||
if \!.y then iterate j /*does substring of |
if \!.y then iterate j /*does substring of P not prime? Skip.*/ |
||
end /*m*/ |
end /*m*/ |
||
end /*k*/ |
end /*k*/ |
||
found= found + 1 /*bump the number of substring primes. */ |
found= found + 1 /*bump the number of substring primes. */ |
||
$= $ right( commas( |
$= $ right( commas(p), w) /*add a substring prime ──► $ list. */ |
||
if found//cols\==0 then iterate /*have we populated a line of output? */ |
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). */ |
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ |
||
Line 589: | Line 589: | ||
#=5; s.#= @.# **2 /*number of primes so far; prime². */ |
#=5; s.#= @.# **2 /*number of primes so far; prime². */ |
||
/* [↓] generate more primes ≤ high.*/ |
/* [↓] generate more primes ≤ high.*/ |
||
do j=@.#+2 by 2 to n |
do j=@.#+2 by 2 to n-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 s.k<=j /* [↓] divide by the known odd primes.*/ |
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. ___ */ |
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ |
||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
||
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
||
end /*j*/; return</lang> |
end /*j*/; return</lang> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
index │ primes (base ten) where all substrings are also primes, where N < 500 |
index │ primes (base ten) where all substrings are also primes, where N < 500 |
||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
||
1 │ |
1 │ 2 3 5 7 23 37 53 73 373 |
||
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
||