Project Euler 27

My solution to yet another Euler Problem

(I apologize to any Perl Programmers who see this, I learn Perl about 4 days ago)

#!/usr/bin/perl

$a =1;

$b =NextPrime(811); # |B| must be prime

print $b;

$best_consec =0;
$best_a =0;
$best_b =0;

for ($a =-1000 ; $a< 1000 ; $a++)
{

for ($b =2 ; $b < 1000 ; $b =NextPrime($b) ) { # gives a & b #print "Current a:$a b:$bn"; my $n=0; my $consec =0; RES: while() { $result = ($n*$n) + ($a*$n) +$b; if(isPrime ($result)) { $consec++; #print "n: $n results in prime : $result, so adding 1, Consec now = $consecn"; } else { if($consec>$best_consec)
{
$best_a =$a;
$best_b =$b;
$best_consec= $consec;
print "NEW BEST: a:$best_a b:$best_b => $consecn";
}
last;
}
$n++;
}

}

}

sub isPrime()
{
my $test_val = $_[0];

return 0 if $test_val <1;

for (my $i=2;$i {
return 0 if ($test_val % $i) eq 0;
}
return 1;
}

sub NextPrime()
{
#Try the next number
NUMBER: for (my $num=$_[0]+1; $num<1500; $num++)
{

DIV: for (my $i=2;$i {
next NUMBER if ($num % $i) eq 0;
}
return $num;
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *