Primes whose first and last number is 3: Difference between revisions
Content added Content deleted
m (added whitespace and the requirement of base ten numbers.) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 88: | Line 88: | ||
9349 9419 9439 9479 9539 9619 9629 9649 9679 9689 |
9349 9419 9439 9479 9539 9619 9629 9649 9679 9689 |
||
9719 9739 9749 9769 9829 9839 9859 9929 9949</pre> |
9719 9739 9749 9769 9829 9839 9859 9929 9949</pre> |
||
=={{header|REXX}}== |
|||
<lang ring>/*REXX pgm finds and displays primes (base ten) that contain a leading and trailing 3. */ |
|||
parse arg hi cols dig . /*obtain optional argument from the CL.*/ |
|||
if hi=='' | hi=="," then hi= 4000 /*Not specified? Then use the default.*/ |
|||
if cols=='' | cols=="," then cols= 10 /* " " " " " " */ |
|||
if dig=='' | dig=="," then dig= 3 /* " " " " " " */ |
|||
call genP /*build array of semaphores for primes.*/ |
|||
w= 10 /*width of a number in any column. */ |
|||
title= ' primes N (in base ten) that have a leading and ending digit of ' dig , |
|||
" where N < " commas(hi) |
|||
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) ) |
|||
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') |
|||
found= 0; idx= 1 /*initialize # of primes found; IDX. */ |
|||
$= /*list of primes that contain a string.*/ |
|||
do j=1 for # /*find primes with leading/trailing dig*/ |
|||
parse var @.j Ld 2 '' -1 Td /*obtain the leading and trailing digit*/ |
|||
if Ld\==dig then iterate /*does prime contain a leading DIG ? */ /* ◄■■■■■■■ a filter.*/ |
|||
if Td\==dig then iterate /* " " " " trailing ? ? */ /* ◄■■■■■■■ a filter.*/ |
|||
found= found + 1 /*bump the number of primes found. */ |
|||
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 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(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
|||
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') |
|||
say |
|||
say 'Found ' commas(found) title |
|||
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: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */ |
|||
!.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " semaphores. */ |
|||
#=5; sq.#= @.# **2 /*number of primes so far; prime². */ |
|||
do j=@.#+2 by 2 to hi-1 /*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? */ |
|||
do k=5 while sq.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; sq.#= 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 N (in base ten) that have a leading and ending digit of 3 where N < 4,000 |
|||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
1 │ 3 313 353 373 383 3,023 3,083 3,163 3,203 3,253 |
|||
11 │ 3,313 3,323 3,343 3,373 3,413 3,433 3,463 3,533 3,583 3,593 |
|||
21 │ 3,613 3,623 3,643 3,673 3,733 3,793 3,803 3,823 3,833 3,853 |
|||
31 │ 3,863 3,923 3,943 |
|||
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
Found 33 primes N (in base ten) that have a leading and ending digit of 3 where N < 4,000 |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |