Posted By

cmm7825 on 05/20/10


Tagged


Versions (?)

Problem 10


 / Published in: Perl
 

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my @nums;
  6. my $n = 2000000;
  7. my ($p, $j, $sum);
  8.  
  9. $nums[0] = 0;
  10. foreach my $i (1..$n-1)
  11. {
  12. $nums[$i] = 1;
  13. }
  14. $p = 2;
  15. while($p**2 <= $n)
  16. {
  17. $j = $p**2;
  18. while($j <= $n)
  19. {
  20. $nums[$j] = 0;
  21. $j = $j+$p;
  22. }
  23. do{$p++;} until($nums[$p] == 1);
  24. }
  25.  
  26. foreach my $num (0..$n-1){
  27. $sum += $num if $nums[$num] == 1;}
  28.  
  29. $sum--;
  30. print "$sum\n";

Report this snippet  

You need to login to post a comment.