Coprime triplets: Difference between revisions

m
→‎{{header|REXX}}: simplified code.
(→‎{{header|REXX}}: optimized and simplified the code.)
m (→‎{{header|REXX}}: simplified code.)
Line 114:
if cols>0 then say ' index │'center(@copt, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(W+1), '─')
!.= 0; @.= !.; idx= 1; $= /*initialize some variables. */
do #=1
do j=1; if @.j then iterate /*J in list of coprime triplets? Skip.*/
if #<3 then leave /*First two entries not definedefined? Use it.*/
if gcd(j has( a= # -1) )\==1; then iterate b= # /*is- J2 coprime with last number? /*get the last two indices of sequence.*/
if gcd(j, has(#-2) !.a)\==1 then iterate /* " " /*J not "coprime with last " penultimate number?*/
leave if gcd(j, !.b)\==1 then iterate /*" " " " penultimate " /*OK, we've found a new coprime triplet*/
if j>=n then leave /*HaveOK, we've exceededfound thea limit?new Thencoprime quittriplet*/
end /*j*/
if j>=n then leave /*Have we exceeded the limit? Then quit*/
@. if j>=n 1; then leave !.#= j /*flagHave we aexceeded coprimethe tripletlimit? (twoThen methods).quit*/
if cols@.j==0 1; then iterate !.#= j /*Notflag showinga thecoprime numbers?triplet Keep(two lookingmethods).*/
$= $if cols==0 right(then commas(j),iterate w) /*appendNot coprimeshowing tripletthe tonumbers? outputKeep listlooking*/
if #$= //$ cols \==right( commas(j), w) 0 then iterate /*Isappend outputcoprime linetriplet full?to No, keepoutput lookinglist*/
say center(idx,if #//cols\==0 then iterate 7)'│' substr($, 2); $= /*showIs output line offull? No, coprimekeep triplets.looking*/
idx= say center(idx, + cols 7)'│' substr($, 2); $= /*bumpshow theoutput indexline forof thecoprime output linetriplets. */
idx= idx + cols /*bump the index for the output line. */
end /*forever*/
 
if $\=='' then say center(idx, 7)'│' substr($, 2) /*show any residual output numbers*/
Line 138 ⟶ 139:
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
gcd: procedure; parse arg x ,y; do until _==0; _= x//y; x= y; y= _; end; return x</lang>
has: parse arg _; return !._</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>