Triplet of three numbers: Difference between revisions
Content added Content deleted
(Added Sidef) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed algorithm.) |
||
Line 1,313: | Line 1,313: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX pgm finds prime triplets |
<lang rexx>/*REXX pgm finds prime triplets: n-1, n+3, n+5 are primes, and n < some specified #.*/ |
||
parse arg hi . |
parse arg hi cols . /*obtain optional argument from the CL.*/ |
||
if hi=='' | hi=="," then hi= 6000 |
if hi=='' | hi=="," then hi= 6000 /*Not specified? Then use the default.*/ |
||
if cols=='' | cols=="," then cols= 4 /* " " " " " " */ |
|||
call genP hi + 5 /*build semaphore array for low primes.*/ |
|||
w= 30 /*width of a prime triplet in a column.*/ |
|||
__= ' '; title= ' prime triplets: n-1, n+3, n+5 are primes, and n < ' commas(hi) |
|||
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) ) |
|||
say '───────┼'center("" |
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') |
||
found= 0 |
found= 0; idx= 1 /*initialize # prime triplets & index.*/ |
||
$= /*a list of prime triplets (so far). */ |
|||
do j=1 for hi-1 /*look for prime triplets within range.*/ |
|||
p1= j - 1; if \!.p1 then iterate /*Is P1 not prime? Then skip it. */ |
|||
p3= j + 3; if \!.p3 then iterate /* " P3 " " " " " */ |
|||
p5= j + 5; if \!.p5 then iterate /* " P5 " " " " " */ |
|||
found= found + 1 /*bump the number of prime triplets. */ |
|||
if cols<0 then iterate /*Build the list (to be shown later)? */ |
|||
⚫ | |||
ttt= commas(p1)__ commas(p3)__ commas(p5) /*add commas & blanks to prime triplet.*/ |
|||
$= $ left( '('ttt")", w) /*add a prime triplet ──► the $ list.*/ |
|||
if found//cols\==0 then iterate /*have we populated a line of output? */ |
|||
say center(idx, 7)'│' strip(substr($, 2), 'T'); $= /*show what we have so far.*/ |
|||
idx= idx + cols /*bump the index count for the output*/ |
|||
⚫ | |||
if $\=='' then say center(idx, 7)"│" strip(substr($, 2), 'T') /*possible show residual*/ |
|||
say '───────┴'center("" , 1 + ( |
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') |
||
say |
say |
||
say 'Found ' |
say 'Found ' commas(found) title |
||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
commas: |
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? |
||
isPrime: parse arg ?; return !.? |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
genP: |
genP: !.= 0; parse arg hip /*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 hip /*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? */ |
||
if j// 7==0 then iterate /*" " " 7? */ |
|||
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. ___ */ |
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ |
||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
||
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
||
end /*j*/; return</lang> |
end /*j*/; return</lang> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
index │ prime triplets: n-1, n+3, n+5 are primes, and n < 6,000 |
|||
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
───────┼──────────────────────────────────────────────────────────────────────── |
|||
1 │ (7 11 13) (13 17 19) (37 41 43) (67 71 73) |
|||
5 │ (97 101 103) (103 107 109) (193 197 199) (223 227 229) |
|||
9 │ (277 281 283) (307 311 313) (457 461 463) (613 617 619) |
|||
13 │ (823 827 829) (853 857 859) (877 881 883) (1,087 1,091 1,093) |
|||
17 │ (1,297 1,301 1,303) (1,423 1,427 1,429) (1,447 1,451 1,453) (1,483 1,487 1,489) |
|||
21 │ (1,663 1,667 1,669) (1,693 1,697 1,699) (1,783 1,787 1,789) (1,867 1,871 1,873) |
|||
25 │ (1,873 1,877 1,879) (1,993 1,997 1,999) (2,083 2,087 2,089) (2,137 2,141 2,143) |
|||
29 │ (2,377 2,381 2,383) (2,683 2,687 2,689) (2,707 2,711 2,713) (2,797 2,801 2,803) |
|||
33 │ (3,163 3,167 3,169) (3,253 3,257 3,259) (3,457 3,461 3,463) (3,463 3,467 3,469) |
|||
37 │ (3,847 3,851 3,853) (4,153 4,157 4,159) (4,513 4,517 4,519) (4,783 4,787 4,789) |
|||
41 │ (5,227 5,231 5,233) (5,413 5,417 5,419) (5,437 5,441 5,443) (5,647 5,651 5,653) |
|||
45 │ (5,653 5,657 5,659) (5,737 5,741 5,743) |
|||
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
824 │ (N-1)= 823 (N+3)= 827 (N+5)= 829 |
|||
854 │ (N-1)= 853 (N+3)= 857 (N+5)= 859 |
|||
878 │ (N-1)= 877 (N+3)= 881 (N+5)= 883 |
|||
1,088 │ (N-1)= 1,087 (N+3)= 1,091 (N+5)= 1,093 |
|||
1,298 │ (N-1)= 1,297 (N+3)= 1,301 (N+5)= 1,303 |
|||
1,424 │ (N-1)= 1,423 (N+3)= 1,427 (N+5)= 1,429 |
|||
1,448 │ (N-1)= 1,447 (N+3)= 1,451 (N+5)= 1,453 |
|||
1,484 │ (N-1)= 1,483 (N+3)= 1,487 (N+5)= 1,489 |
|||
1,664 │ (N-1)= 1,663 (N+3)= 1,667 (N+5)= 1,669 |
|||
1,694 │ (N-1)= 1,693 (N+3)= 1,697 (N+5)= 1,699 |
|||
1,784 │ (N-1)= 1,783 (N+3)= 1,787 (N+5)= 1,789 |
|||
1,868 │ (N-1)= 1,867 (N+3)= 1,871 (N+5)= 1,873 |
|||
1,874 │ (N-1)= 1,873 (N+3)= 1,877 (N+5)= 1,879 |
|||
1,994 │ (N-1)= 1,993 (N+3)= 1,997 (N+5)= 1,999 |
|||
2,084 │ (N-1)= 2,083 (N+3)= 2,087 (N+5)= 2,089 |
|||
2,138 │ (N-1)= 2,137 (N+3)= 2,141 (N+5)= 2,143 |
|||
2,378 │ (N-1)= 2,377 (N+3)= 2,381 (N+5)= 2,383 |
|||
2,684 │ (N-1)= 2,683 (N+3)= 2,687 (N+5)= 2,689 |
|||
2,708 │ (N-1)= 2,707 (N+3)= 2,711 (N+5)= 2,713 |
|||
2,798 │ (N-1)= 2,797 (N+3)= 2,801 (N+5)= 2,803 |
|||
3,164 │ (N-1)= 3,163 (N+3)= 3,167 (N+5)= 3,169 |
|||
3,254 │ (N-1)= 3,253 (N+3)= 3,257 (N+5)= 3,259 |
|||
3,458 │ (N-1)= 3,457 (N+3)= 3,461 (N+5)= 3,463 |
|||
3,464 │ (N-1)= 3,463 (N+3)= 3,467 (N+5)= 3,469 |
|||
3,848 │ (N-1)= 3,847 (N+3)= 3,851 (N+5)= 3,853 |
|||
4,154 │ (N-1)= 4,153 (N+3)= 4,157 (N+5)= 4,159 |
|||
4,514 │ (N-1)= 4,513 (N+3)= 4,517 (N+5)= 4,519 |
|||
4,784 │ (N-1)= 4,783 (N+3)= 4,787 (N+5)= 4,789 |
|||
5,228 │ (N-1)= 5,227 (N+3)= 5,231 (N+5)= 5,233 |
|||
5,414 │ (N-1)= 5,413 (N+3)= 5,417 (N+5)= 5,419 |
|||
5,438 │ (N-1)= 5,437 (N+3)= 5,441 (N+5)= 5,443 |
|||
5,648 │ (N-1)= 5,647 (N+3)= 5,651 (N+5)= 5,653 |
|||
5,654 │ (N-1)= 5,653 (N+3)= 5,657 (N+5)= 5,659 |
|||
5,738 │ (N-1)= 5,737 (N+3)= 5,741 (N+5)= 5,743 |
|||
───────┴──────────────────────────────────────────────────────────────────────── |
|||
Found 46 prime triplets |
Found 46 prime triplets: n-1, n+3, n+5 are primes, and n < 6,000 |
||
</pre> |
</pre> |
||