Jump to content

Substring primes: Difference between revisions

m
→‎{{header|REXX}}: printed a better grid, made program more generic.
m (→‎{{header|REXX}}: printed a better grid, made program more generic.)
Line 456:
=={{header|REXX}}==
<lang rexx>/*REXX program finds/shows decimal primes where all substrings are also prime, N < 500.*/
parse arg hin cols . /*obtain optional argument from the CL.*/
if hi n=='' | hi n=="," then hi n= 500 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
call genP /*build array of semaphores for primes.*/
w= 7 10 /*width of a number in any column. */
@sprstitle= ' primes (base ten) where all substrings are also primes, where < 'N < ' hicommas(n)
say ' index │'center(@sprstitle, 1 + cols*(w+1) ) /*display the title of the output. */
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " " separator " " " */
genP: !.found= 0; ptests idx= 01 /*placeholdersinitialize for# of nice primes (semaphores).and index*/
$= /*a list of substring primes (so far). */
do j=1 for #; x= @.j; x2= substr(x, 2) /*search for primes that fit criteria. */
Line 475 ⟶ 476:
end /*k*/
 
$found= $found + right(x,1 w) /*addbump the number Xof primenice to theprimes. $ list. */
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*/
 
if $\=='' then say center(1idx, 7)"'"' substr($, 2) /*display the list ofany substringresidual primesnumbers.*/
say; '───────┴'center("" , 1 + cols*(w+1), '─') say 'Found ' /* " words($) a foot separator. @sprs*/
say; say 'Found ' words($) title /* " the summary. */
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; ptests= 0 /*placeholders for primes (semaphores).*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 hin /*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? */
Line 499 ⟶ 509:
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index │ primes (base ten) where all substrings are also primes, where N < 500
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┼─────────────────────────────────────────────────────────────────────────────────
1 │ 1 2 3 5 4 9 7 12 23 16 37 53 21 73 37374
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
 
Found 9 primes (base ten) where all substrings are also primes, where N < 500
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.