Jump to content

Triplet of three numbers: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, changed algorithm.
(Added Sidef)
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed algorithm.)
Line 1,313:
 
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds prime triplets: (Alabama primes)n-1, suchn+3, thatn+5 P,are P-1primes, and P+2 n are< primessome specified #.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 6000 /*Not specified? Then use the default.*/
callif genPcols=='' | cols=="," then cols= 4 /* " " " " " /*build array" of semaphores for primes.*/
w=call length(genP commas(hi) )+ 5 /*the width of the largest number. /*build semaphore array for low primes.*/
oww= 70;30 pad= left('', w + 1) /*the width of the data column. /*width of a prime triplet in a column.*/
@primTrip__= ' prime'; triplets such thattitle= ' prime triplets: Nn-1, Nn+3, Nn+5 are primeprimes, Nand n < ' commas(hi)
sayif 'cols>0 then Nsay ' index │'center(@primTriptitle, 1 + cols*(oww+1) ) /*display the title for the output grid*/
if cols>0 then say '───────┼'center("" , , 1 + cols*(oww+1), '─') /* " " header " " " " */
found= 0; idx= 1 /*initialize # of prime triplets. & index.*/
$= do j=1 for hi-1 /*find some prime triplets < HI. /*a list of prime triplets (so far). */
do j=1 for if \isPrime(jhi-1) | \isPrime(j+3) | \isPrime(j+5) then iterate /*¬look for prime? triplets within range.*/
foundp1= foundj +- 1; if \!.p1 then iterate /*Is P1 not prime? /*bump theThen numberskip of prime tripletsit. */
p3= j + 3; say center(commas(j),if 7)'│'\!.p3 then iterate pad /* "(N-1)= P3 " right( commas(j-1)," w) , " " " */
p5= j + 5; if \!.p5 then iterate /* " P5 " " pad " '(N+3)=' " right( commas(j+3), w)" ,*/
found= found + 1 /*bump the number of pad prime triplets. "(N+5)=" right( commas(j+5), w)*/
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*/
if cols>0 then say '───────┴'center("" , 1 + cols*(oww+1), '─') /*display foot header for output grid. */
say
say 'Found ' commas(found) @primTrip 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 ?
isPrime: parse arg ?; return !.?
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: @!.1=2 0; @.2=3; @.3=5; @.4=7 /*defineparse somearg lowhip primes. /*placeholders for primes (semaphores).*/
!@.1=02; !@.2=13; !@.3=15; !@.54=17; !@.75=1 11 /*define some low "primes. " " " semaphores. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " #=4; " s.#= @.#" **2 " /*number of primes so far;flags. prime². */
do j=@.#+2 by 2 to hi #=5; s.#= @.# **2 /*findnumber oddof primes where P < hi.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)*/
if j// 3==0 then iterate /*" " " 3? */
do k=4 while s.k<=j if j// 7==0 then iterate /*" " " 7? [↓] divide by the known odd primes.*/
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. ___ */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index N prime triplets such that N prime triplets: n-1, Nn+3, Nn+5 are primeprimes, Nand n < 6,000
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┼────────────────────────────────────────────────────────────────────────
81 (7 11 13) (N-113 17 19)= 7 (N+337 41 43)= 11 (N+5)=67 71 1373)
14 5 (97 101 103) (N-1103 107 109)= 13 (N+3193 197 199)= 17 (N+5)=223 227 19229)
38 9 (277 281 283) (N-1307 311 313)= 37 (N+3457 461 463)= 41 (N+5)=613 617 43619)
6813 (823 827 829) (N-1853 857 859)= 67 (N+3877 881 883)= 71 (N+5)=1,087 1,091 731,093)
9817 (1,297 1,301 1,303) (N-1)=,423 1,427 1,429) 97 (N+3)=1,447 1,451 1,453) 101 (N+5)=1,483 1,487 103 1,489)
10421 (1,663 1,667 1,669) (N-1)=,693 1,697 1,699) 103 (N+3)=1,783 1,787 1,789) 107 (N+5)=1,867 1,871 109 1,873)
19425 (1,873 1,877 1,879) (N-1)=,993 1,997 1,999) 193 (N+3)=2,083 2,087 2,089) 197 (N+5)=2,137 2,141 199 2,143)
22429 (2,377 2,381 2,383) (N-1)=2,683 2,687 2,689) 223 (N+3)=2,707 2,711 2,713) 227 (N+5)=2,797 2,801 229 2,803)
27833 (3,163 3,167 3,169) (N-1)=3,253 3,257 3,259) 277 (N+3)=,457 3,461 3,463) 281 (N+5)=3,463 3,467 283 3,469)
30837 (3,847 3,851 3,853) (N-1)=4,153 4,157 4,159) 307 (N+3)=4,513 4,517 4,519) 311 (N+5)=4,783 4,787 313 4,789)
45841 (5,227 5,231 5,233) (N-1)=5,413 5,417 5,419) 457 (N+3)=5,437 5,441 5,443) 461 (N+5)=,647 5,651 463 5,653)
61445 (5,653 5,657 (N-15,659)= 613 (N+3)=5,737 5,741 617 (N+5,743)= 619
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
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 Nn-1, Nn+3, Nn+5 are primeprimes, Nand n < 6,000
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.