use strict; use warnings; my $tri = 1; my $i = 1; while ($i++) { $tri += $i; my $nof = 2 * scalar(grep { $tri % $_ == 0 } 1 .. sqrt($tri)); if ($nof >= 500) { print "The " . $i . "th triangle number $tri has $nof divisors"; last; } } __END__ $ time perl -wl 12.pl The 12375th triangle number 76576500 has 576 divisors real 0m26.947s user 0m25.013s sys 0m0.027s