answer to question #4 from the Euler project, in Perl