diff options
author | Jesse Luehrs <doy@tozt.net> | 2009-05-18 00:04:45 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2009-05-18 00:04:45 -0500 |
commit | 37c4f77fe7c41603b9410728ccd53b9021749717 (patch) | |
tree | 72607f061be69b904bcf4ea542c6d585c1220929 | |
parent | fada6c2c86e3c5d0673c193f028fe87879b34e14 (diff) | |
download | projecteuler-37c4f77fe7c41603b9410728ccd53b9021749717.tar.gz projecteuler-37c4f77fe7c41603b9410728ccd53b9021749717.zip |
problem 27 solution
-rwxr-xr-x | 027.pl | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -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"; |