Matt Owen

source code for "files/pe/pe_005.pl"

return to portfolio
  1.  #!/usr/bin/perl
  2.  
  3.  use strict;
  4.  use warnings;
  5.  
  6.  sub prime_sieve {
  7.   my ($n, $i, $j) = (shift, 0, 0);
  8.   my @sieve = (2, 3);
  9.  
  10.   $n = $n > 3 ? int $n : 3;
  11.  
  12.   for ($i=6; $i < $n; $i+= 6) {
  13.   push(@sieve, $i-1, $i+1);
  14.   }
  15.  
  16.   for $i (0..int @sieve-1) {
  17.   for $j ($i+1..int @sieve-1) {
  18.   if ($sieve[$j] % $sieve[$i] == 0) {
  19.   $sieve[$j] = 1;
  20.   }
  21.   }
  22.   }
  23.  
  24.   return @sieve;
  25.  }
  26.  
  27.  
  28.  # INCOMPLETE.... JUST DID THIS BY HAND, BECAUSE...