# harmonic($n) computes the sum of the first $n terms of the
#   harmonic series.
#
sub harmonic {
    my ($n) = shift;
    my ($i, $result);
    for ($i = 1; $i <= $n; $i++) {
        $result += 1/$i;
    }
    return $result;
}

# harmonic_approx($n) computes an approximate sum of the first $n
#   terms of the harmonic series.
#
sub harmonic_approx {
    my ($n) = shift;
    return log($n) +
        0.577215664901532 +
            (1 / (2 * $n)) -
                (1 / (12 * ($n ** 2))) +
                    (1 / (120 * ($n ** 4)));
}

for (1..5) {
    print harmonic($_), "\n";
    print harmonic_approx($_), "\n";
}
