Closest-pair problem: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: optimized the inner DO loop.)
Line 4,193: Line 4,193:
if high=='' | high=="," then high= 20000 /* " " " " " " */
if high=='' | high=="," then high= 20000 /* " " " " " " */
if datatype(seed, 'W') then call random ,,seed /*seed for RANDOM (BIF) repeatability.*/
if datatype(seed, 'W') then call random ,,seed /*seed for RANDOM (BIF) repeatability.*/
w=length(high); w=w + (w//2==0) /*W: for aligning the output columns.*/
w= length(high); w= w + (w//2==0) /*W: for aligning the output columns.*/
/*╔══════════════════════╗*/ do j=1 for N /*generate N random points*/
/*╔══════════════════════╗*/ do j=1 for N /*generate N random points*/
/*║ generate N points. ║*/ @x.j= random(low, high) /* " a random X */
/*║ generate N points. ║*/ @x.j= random(low, high) /* " a random X */
/*╚══════════════════════╝*/ @y.j= random(low, high) /* " " " Y */
/*╚══════════════════════╝*/ @y.j= random(low, high) /* " " " Y */
end /*j*/ /*X & Y make the point.*/
end /*j*/ /*X & Y make the point.*/
A=1; B=2 /* [↓] MINDD is actually the squared*/
A= 1; B= 2 /* [↓] MINDD is actually the squared*/
minDD= (@x.A - @x.B)**2 + (@y.A - @y.B)**2 /*distance between the first two points*/
minDD= (@x.A - @x.B)**2 + (@y.A - @y.B)**2 /*distance between the first two points*/
/* [↓] use of XJ & YJ speed things up.*/
/* [↓] use of XJ & YJ speed things up.*/
do j=1 for N-1; xj= @x.j; yj= @y.j /*find minimum distance between a ··· */
do j=1 for N-1; xj= @x.j; yj= @y.j /*find minimum distance between a ··· */
do k=j+1 to N /* ··· point and all the other points.*/
do k=j+1 for N-j-1 /* ··· point and all the other points.*/
dd= (xj - @x.k)**2 + (yj - @y.k)**2 /*compute squared distance from points.*/
dd= (xj - @x.k)**2 + (yj - @y.k)**2 /*compute squared distance from points.*/
if dd<minDD then parse value dd j k with minDD A B
if dd<minDD then parse value dd j k with minDD A B
Line 4,211: Line 4,211:
say left('', length($) - 1) "["right(@x.A, w)',' right(@y.A, w)"]"
say left('', length($) - 1) "["right(@x.A, w)',' right(@y.A, w)"]"
say left('', length($) - 1) "["right(@x.B, w)',' right(@y.B, w)"]"
say left('', length($) - 1) "["right(@x.B, w)',' right(@y.B, w)"]"
exit /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6