#!/usr/bin/perl

# best_line(\@x_values, \@y_values) calculates the line that most
# nearly passes through all the [x, y] points.
# It returns two values, $a and $b, which define the line $y = $b * $x + $a.
#
sub best_line {
    my ($array1ref, $array2ref) = @_;
    my ($i, $product, $sum1, $sum2, $sum1_squares, $a, $b);
    for ($i = 0; $i < @$array1ref; $i++) {
        $product += $array1ref->[$i] * $array2ref->[$i];
        $sum1 += $array1ref->[$i];
        $sum1_squares += $array1ref->[$i] ** 2;
        $sum2 += $array2ref->[$i];
    }
    $b = ((@$array1ref * $product) - ($sum1 * $sum2)) /
        ((@$array1ref * $sum1_squares) - ($sum1 ** 2));
    $a = ($sum2 - $b * $sum1) / @$array1ref;
    return ($b, $a);
}

@hits  = (2378, 4024, 9696, 7314, 7710);
@sales = (310.94, 315.88, 514.15, 500.18, 610.37);

($b, $a) = best_line(\@hits, \@sales);

print "Best line is y = ${b}x + $a.\n";

@hits  = (3, 5, 7, 9, 11);
@sales = (1, 2, 3, 4, 5);

($b, $a) = best_line(\@hits, \@sales);

print "Best line is y = ${b}x + $a.\n";