Triplet of three numbers: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (→{{header|Cowgol}}: fix typo) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 431: | Line 431: | ||
5654: (5653 5657 5659) |
5654: (5653 5657 5659) |
||
5738: (5737 5741 5743)</pre> |
5738: (5737 5741 5743)</pre> |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX pgm finds prime triplets (Alabama primes) such that P, P-1, and P+2 are primes.*/ |
|||
parse arg hi cols . /*obtain optional argument from the CL.*/ |
|||
if hi=='' | hi=="," then hi= 6000 /*Not specified? Then use the default.*/ |
|||
call genP /*build array of semaphores for primes.*/ |
|||
w= length( commas(hi) ) /*the width of the largest number. */ |
|||
ow= 60; pad= left('', w) /*the width of the data column. */ |
|||
@primTrip= ' prime triplets such that N-1, N+3, N+5 are prime, N<' commas(hi) |
|||
say ' N │'center(@primTrip, 1 + (ow+1) ) /*display the title for the output grid*/ |
|||
say '───────┼'center("" , 1 + (ow+1), '─') /* " " header " " " " */ |
|||
found= 0 /*initialize # of prime triplets. */ |
|||
do j=1 for hi-1; jm1= j - 1 /*find some prime triplets < HI. */ |
|||
if \!.jm1 then iterate; jp3= j + 3 |
|||
if \!.jp3 then iterate; jp5= j + 5 |
|||
if \!.jp5 then iterate |
|||
found= found + 1 /*bump the number of prime triplets. */ |
|||
say center(commas(j), 7)'│' pad "(N-1)=" right( commas(jm1), w) , |
|||
pad '(N+3)=' right( commas(jp3), w) , |
|||
pad "(N+5)=" right( commas(jp5), w) |
|||
end /*j*/ |
|||
say '───────┴'center("" , 1 + (ow+1), '─') /*display foot header for output grid. */ |
|||
say |
|||
say 'Found ' commas(found) @primTrip |
|||
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 /*prime semaphores; sP= sum of primes.*/ |
|||
@.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 until @.#>=hi /*find odd primes where P≥hi and P>sP.*/ |
|||
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? */ |
|||
/* [↑] the above five lines saves time*/ |
|||
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= when using the default inputs:}} |
|||
<pre> |
|||
N │ prime triplets such that N-1, N+3, N+5 are prime, N< 6,000 |
|||
───────┼────────────────────────────────────────────────────────────── |
|||
8 │ (N-1)= 7 (N+3)= 11 (N+5)= 13 |
|||
14 │ (N-1)= 13 (N+3)= 17 (N+5)= 19 |
|||
38 │ (N-1)= 37 (N+3)= 41 (N+5)= 43 |
|||
68 │ (N-1)= 67 (N+3)= 71 (N+5)= 73 |
|||
98 │ (N-1)= 97 (N+3)= 101 (N+5)= 103 |
|||
104 │ (N-1)= 103 (N+3)= 107 (N+5)= 109 |
|||
194 │ (N-1)= 193 (N+3)= 197 (N+5)= 199 |
|||
224 │ (N-1)= 223 (N+3)= 227 (N+5)= 229 |
|||
278 │ (N-1)= 277 (N+3)= 281 (N+5)= 283 |
|||
308 │ (N-1)= 307 (N+3)= 311 (N+5)= 313 |
|||
458 │ (N-1)= 457 (N+3)= 461 (N+5)= 463 |
|||
614 │ (N-1)= 613 (N+3)= 617 (N+5)= 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 N-1, N+3, N+5 are prime, N< 6,000 |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |