#!/usr/bin/perl

sub factorial {
    my ($n, $res) = (shift, 1);

    # Nonintegers require the gamma function,
    # discussed later in the chapter.
    return undef unless $n >= 0 and $n == int($n);

    $res *= $n-- while $n > 1;
    return $res;
}
