challenges

my solutions to various "programming challenge" problems
git clone https://wehaveforgeathome.hates.computer/challenges.git
Log | Files | Refs | LICENSE

007.pl (392B)


      1 #!/usr/bin/perl
      2 
      3 use strict;
      4 use warnings;
      5 
      6 my $nth_prime = 10001;
      7 
      8 my $counter = 1;
      9 my $i = 3;
     10 my $last = 1;
     11 
     12 while ($counter < $nth_prime) {
     13   if (is_prime_over_2($i)) {
     14     $counter++;
     15     $last = $i;
     16   }
     17   $i += 2;
     18 }
     19 
     20 print "$last\n";
     21 
     22 sub is_prime_over_2 {
     23   my $n = shift;
     24   my $mid = sqrt($n);
     25   for (my $i = 3; $i <= $mid; $i += 2) {
     26     return 0 if ($n % $i == 0);
     27   }
     28   return 1;
     29 }