Created
August 18, 2012 06:40
-
-
Save ichirin2501/3384958 to your computer and use it in GitHub Desktop.
Project Euler 27
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
sub is_prime{ | |
my $n = shift; | |
return 0 if $n < 2; | |
return 0 if $n % 2 == 0 && $n > 2; | |
for(my $i = 3; $i*$i <= $n; $i += 2){ | |
return 0 if $n % $i == 0; | |
} | |
return 1; | |
} | |
sub seq_prime_count{ | |
my ($a, $b) = @_; | |
my $n = 0; | |
while( is_prime($n**2 + $a*$n + $b) ){ | |
$n++; | |
} | |
return $n; | |
} | |
my ($a, $b); | |
my $mx = 0; | |
foreach my $j (2..999){ | |
foreach my $i ((1-$j)..999){ | |
my $tmp = seq_prime_count($i, $j); | |
if( $mx < $tmp ){ | |
$mx = $tmp; | |
($a, $b) = ($i, $j); | |
} | |
} | |
} | |
print $a * $b,"\n"; | |
# => -59231 | |
# ぜんぜんperlっぽくないな… |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment