in reply to finding perfect numbers

Using the power of CPAN:
use strict; use 5.012; use Math::Factor::XS qw/factors/; use List::Util qw/sum/; for (2 ... 10000) { say "$_ is perfect" if sum(factors($_)) == $_ - 1; }

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: finding perfect numbers
by hexcoder (Curate) on Aug 05, 2010 at 21:44 UTC
    Thanks, this variant even works with warnings enabled and older Perls:
    use strict; use warnings; use Math::Factor::XS qw/factors/; use List::Util qw/sum/; for (2 .. 10000) { print "$_ is perfect\n" if (sum(factors($_),0) == $_ - 1); }
      Well done!

      And that made me think of the following small change:

      print "$_ is perfect\n" if (sum(factors($_),1) == $_);
      Thanks for putting me on that track!

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James