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 (Alabama primes) such that P, P-1, and P+2 are primes.*/
<lang rexx>/*REXX pgm finds prime triplets: n-1, n+3, n+5 are primes, and n < some specified #.*/
parse arg hi . /*obtain optional argument from the CL.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 6000 /*Not specified? Then use the default.*/
if hi=='' | hi=="," then hi= 6000 /*Not specified? Then use the default.*/
call genP /*build array of semaphores for primes.*/
if cols=='' | cols=="," then cols= 4 /* " " " " " " */
w= length( commas(hi) ) /*the width of the largest number. */
call genP hi + 5 /*build semaphore array for low primes.*/
ow= 70; pad= left('', w + 1) /*the width of the data column. */
w= 30 /*width of a prime triplet in a column.*/
@primTrip= ' prime triplets such that N-1, N+3, N+5 are prime, N < ' commas(hi)
__= ' '; title= ' prime triplets: n-1, n+3, n+5 are primes, and n < ' commas(hi)
say ' N │'center(@primTrip, 1 + (ow+1) ) /*display the title for the output grid*/
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) )
say '───────┼'center("" , 1 + (ow+1), '─') /* " " header " " " " */
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
found= 0 /*initialize # of prime triplets. */
found= 0; idx= 1 /*initialize # prime triplets & index.*/
do j=1 for hi-1 /*find some prime triplets < HI. */
$= /*a list of prime triplets (so far). */
if \isPrime(j-1) | \isPrime(j+3) | \isPrime(j+5) then iterate /*¬ prime? */
do j=1 for hi-1 /*look for prime triplets within range.*/
found= found + 1 /*bump the number of prime triplets. */
p1= j - 1; if \!.p1 then iterate /*Is P1 not prime? Then skip it. */
say center(commas(j), 7)'│' pad "(N-1)=" right( commas(j-1), w) ,
p3= j + 3; if \!.p3 then iterate /* " P3 " " " " " */
pad '(N+3)=' right( commas(j+3), w) ,
p5= j + 5; if \!.p5 then iterate /* " P5 " " " " " */
pad "(N+5)=" right( commas(j+5), w)
found= found + 1 /*bump the number of prime triplets. */
if cols<0 then iterate /*Build the list (to be shown later)? */
end /*j*/
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*/
end /*j*/


if $\=='' then say center(idx, 7)"│" strip(substr($, 2), 'T') /*possible show residual*/
say '───────┴'center("" , 1 + (ow+1), '─') /*display foot header for output grid. */
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say
say 'Found ' commas(found) @primTrip
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: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
isPrime: parse arg ?; return !.?
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: @.1=2; @.2=3; @.3=5; @.4=7 /*define some low primes. */
genP: !.= 0; parse arg hip /*placeholders for primes (semaphores).*/
!.=0; !.2=1; !.3=1; !.5=1; !.7=1 /* " " " " semaphores. */
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
#=4; s.#= @.# **2 /*number of primes so far; prime². */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */
do j=@.#+2 by 2 to hi /*find odd primes where P < hi. */
#=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? */
do k=4 while s.k<=j /* [↓] divide by the known odd primes.*/
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=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
N prime triplets such that N-1, N+3, N+5 are prime, N < 6,000
index prime triplets: n-1, n+3, n+5 are primes, and n < 6,000
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┼────────────────────────────────────────────────────────────────────────
8 │ (N-1)= 7 (N+3)= 11 (N+5)= 13
1 (7 11 13) (13 17 19) (37 41 43) (67 71 73)
14 │ (N-1)= 13 (N+3)= 17 (N+5)= 19
5 (97 101 103) (103 107 109) (193 197 199) (223 227 229)
38 │ (N-1)= 37 (N+3)= 41 (N+5)= 43
9 (277 281 283) (307 311 313) (457 461 463) (613 617 619)
68 │ (N-1)= 67 (N+3)= 71 (N+5)= 73
13 (823 827 829) (853 857 859) (877 881 883) (1,087 1,091 1,093)
98 │ (N-1)= 97 (N+3)= 101 (N+5)= 103
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)
104 │ (N-1)= 103 (N+3)= 107 (N+5)= 109
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)
194 │ (N-1)= 193 (N+3)= 197 (N+5)= 199
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)
224 │ (N-1)= 223 (N+3)= 227 (N+5)= 229
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)
278 │ (N-1)= 277 (N+3)= 281 (N+5)= 283
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)
308 │ (N-1)= 307 (N+3)= 311 (N+5)= 313
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)
458 │ (N-1)= 457 (N+3)= 461 (N+5)= 463
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)
614 (N-1)= 613 (N+3)= 617 (N+5)= 619
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 such that N-1, N+3, N+5 are prime, N < 6,000
Found 46 prime triplets: n-1, n+3, n+5 are primes, and n < 6,000
</pre>
</pre>