Anonymous user
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
if
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
call genP /*build array of semaphores for primes.*/
w=
say ' index │'center(
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. */
Line 475 ⟶ 476:
end /*k*/
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(
say
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
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= when using the default inputs:}}
<pre>
index │ primes (base ten) where all substrings are also primes, where N < 500
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 1 2 3
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
Found 9 primes (base ten) where all substrings are also primes, where N < 500
</pre>
|