solve problem 1 in Perl