Skip to content

Instantly share code, notes, and snippets.

@ichirin2501
Created August 18, 2012 06:40
Show Gist options
  • Save ichirin2501/3384958 to your computer and use it in GitHub Desktop.
Save ichirin2501/3384958 to your computer and use it in GitHub Desktop.
Project Euler 27
#!/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