Problem 6 in Perl