Substring primes: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: printed a better grid, made program more generic.) |
|||
Line 456: | Line 456: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program finds/shows decimal primes where all substrings are also prime, N < 500.*/ |
<lang rexx>/*REXX program finds/shows decimal primes where all substrings are also prime, N < 500.*/ |
||
parse arg |
parse arg n cols . /*obtain optional argument from the CL.*/ |
||
if |
if n=='' | n=="," then n= 500 /*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.*/ |
||
w= |
w= 10 /*width of a number in any column. */ |
||
title= ' primes (base ten) where all substrings are also primes, where N < ' commas(n) |
|||
say ' index │'center( |
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 " " " */ |
||
⚫ | |||
$= /*a list of substring primes (so far). */ |
$= /*a list of substring primes (so far). */ |
||
do j=1 for #; x= @.j; x2= substr(x, 2) /*search for primes that fit criteria. */ |
do j=1 for #; x= @.j; x2= substr(x, 2) /*search for primes that fit criteria. */ |
||
Line 475: | Line 476: | ||
end /*k*/ |
end /*k*/ |
||
found= found + 1 /*bump the number of nice primes. */ |
|||
if cols<0 then iterate /*Build the list (to be shown later)? */ |
|||
c= commas(j) /*maybe add commas to the number. */ |
|||
$= $ right(c, max(w, length(c) ) ) /*add a nice 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 /*j*/ |
end /*j*/ |
||
if $\=='' then say center( |
if $\=='' then say center(idx, 7)'│' substr($, 2) /*display any residual numbers.*/ |
||
say |
say '───────┴'center("" , 1 + cols*(w+1), '─') /* " a foot separator. */ |
||
say; say 'Found ' words($) title /* " the summary. */ |
|||
exit 0 /*stick a fork in it, we're all done. */ |
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: !.= 0 /*placeholders for primes (semaphores).*/ |
|||
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */ |
@.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. */ |
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */ |
||
#=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 |
do j=@.#+2 by 2 to n /*find odd primes from here on. */ |
||
parse var j '' -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/ |
parse var j '' -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/ |
||
if j// 3==0 then iterate /*" " " 3? */ |
if j// 3==0 then iterate /*" " " 3? */ |
||
Line 499: | Line 509: | ||
{{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 < 500 |
index │ primes (base ten) where all substrings are also primes, where N < 500 |
||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
───────┼───────────────────────────────────────────────────────────────────────────────── |
|||
1 │ 2 3 |
1 │ 1 2 3 4 9 12 16 21 74 |
||
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
Found 9 primes (base ten) where all substrings are also primes < 500 |
Found 9 primes (base ten) where all substrings are also primes, where N < 500 |
||
</pre> |
</pre> |
||