Substring primes: Difference between revisions
Content added Content deleted
m (added whitespace.) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 5: | Line 5: | ||
Primes (in base ten) in which all substrings are also primes, where '''n < 500''' |
Primes (in base ten) in which all substrings are also primes, where '''n < 500''' |
||
<br><br> |
<br><br> |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX program finds/shows decimal primes where all substrings are also prime, N < 500.*/ |
|||
parse arg hi cols . /*obtain optional argument from the CL.*/ |
|||
if hi=='' | hi=="," then hi= 500 /*Not specified? Then use the default.*/ |
|||
if cols=='' | cols=="," then cols= 10 /* " " " " " " */ |
|||
call genP /*build array of semaphores for primes.*/ |
|||
w= 7 /*width of a number in any column. */ |
|||
@sprs= ' primes (base ten) where all substrings are also primes < ' hi |
|||
say ' index │'center(@sprs, 1 + cols*(w+1) ) /*display the title of the output. */ |
|||
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " " separator " " " */ |
|||
$= /*a list of substring primes (so far). */ |
|||
do j=1 for #; x= @.j; x2= substr(x, 2) /*search for primes that fit criteria. */ |
|||
if verify(x, 014689, 'M')>0 then iterate /*does X prime have any of these digs?*/ |
|||
if verify(x2, 25 , 'M')>0 then iterate /* " X2 part " " " " " */ |
|||
L= length(x) /*obtain the length of the X prime.*/ |
|||
do k=1 for L-1 /*test for primality for all substrings*/ |
|||
do m=k+1 to L; y= substr(x, k, m-1) /*extract a substring from the X prime.*/ |
|||
if \!.y then iterate j /*does substring of X not prime? Skip.*/ |
|||
end /*m*/ |
|||
end /*k*/ |
|||
$= $ right(x, w) /*add the X prime to the $ list. */ |
|||
end /*j*/ |
|||
if $\=='' then say center(1,7)"│" substr($, 2) /*display the list of substring primes.*/ |
|||
say |
|||
say 'Found ' words($) @sprs |
|||
exit 0 /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
genP: !.= 0 /*placeholders for primes (semaphores).*/ |
|||
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */ |
|||
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */ |
|||
#=5; s.#= @.# **2 /*number of primes so far; prime². */ |
|||
/* [↓] generate more primes ≤ high.*/ |
|||
do j=@.#+2 by 2 to hi /*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? */ |
|||
/* [↑] the above 3 lines saves time.*/ |
|||
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. ___ */ |
|||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
|||
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
|||
end /*j*/; return</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
index │ primes (base ten) where all substrings are also primes < 500 |
|||
───────┼───────────────────────────────────────────────────────────────────────────────── |
|||
1 │ 2 3 5 7 23 37 53 73 373 |
|||
Found 9 primes (base ten) where all substrings are also primes < 500 |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |