#!/usr/bin/perl

# distance( @p ) computes the Euclidean distance between two
# d-dimensional points, given 2 * d coordinates.  For example, a pair of
# 3-D points should be provided as ( $x0, $y0, $z0, $x1, $y1, $z1 ).

sub distance {
    my @p = @_;                 # The coordinates of the points.
    my $d = @p / 2;             # The number of dimensions.

    # The case of two dimensions is optimized.
    return sqrt( ($_[0] - $_[2])**2 + ($_[1] - $_[3])**2 )
        if $d == 2;

    my $S = 0;                  # The sum of the squares.
    my @p0 = splice @p, 0, $d;  # The starting point.

    for ( my $i = 0; $i < $d; $i++ ) {
        my $di = $p0[ $i ] - $p[ $i ];  # Difference...
        $S += $di * $di;                # ...squared and summed.
    }

    return sqrt( $S );
}

print distance( 3,4, 10,12 );
