summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-05-18 00:04:45 -0500
committerJesse Luehrs <doy@tozt.net>2009-05-18 00:04:45 -0500
commit37c4f77fe7c41603b9410728ccd53b9021749717 (patch)
tree72607f061be69b904bcf4ea542c6d585c1220929
parentfada6c2c86e3c5d0673c193f028fe87879b34e14 (diff)
downloadprojecteuler-37c4f77fe7c41603b9410728ccd53b9021749717.tar.gz
projecteuler-37c4f77fe7c41603b9410728ccd53b9021749717.zip
problem 27 solution
-rwxr-xr-x027.pl34
1 files changed, 34 insertions, 0 deletions
diff --git a/027.pl b/027.pl
new file mode 100755
index 0000000..b1f8808
--- /dev/null
+++ b/027.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Math::Prime::XS qw/sieve_primes/;
+
+my @primes = sieve_primes(1e6);
+my %primes = map { $_, 1 } @primes;
+
+sub is_prime {
+ my ($n) = @_;
+ if ($n <= 1e6) {
+ return exists $primes{$n};
+ }
+ return Math::Prime::XS::is_prime($n);
+}
+
+my $max = 0;
+my $max_a = 0;
+my $max_b = 0;
+for my $a (-1000..1000) {
+ for my $b (-1000..1000) {
+ for my $n (0..1e6) {
+ if (!is_prime($n**2+$a*$n+$b)) {
+ if ($n - 1 > $max) {
+ $max = $n - 1;
+ $max_a = $a;
+ $max_b = $b;
+ }
+ last;
+ }
+ }
+ }
+}
+print "$max $max_a $max_b ", $max_a * $max_b, "\n";