Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 43 lines (35 sloc) 0.71 kB
a43e2d4 @jtpalmer Problem 27, Perl 5.
authored
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use 5.010;
5 use Math::Prime::XS qw(is_prime);
76cae85 @jtpalmer Optimize problem 27.
authored
6 use List::MoreUtils qw(any);
a43e2d4 @jtpalmer Problem 27, Perl 5.
authored
7
76cae85 @jtpalmer Optimize problem 27.
authored
8 my %prime;
9 my %not_prime;
a43e2d4 @jtpalmer Problem 27, Perl 5.
authored
10
11 sub _is_prime {
76cae85 @jtpalmer Optimize problem 27.
authored
12 my ($n) = @_;
13
14 return unless $n > 1;
15
16 return if exists $not_prime{$n};
17 return 1 if exists $prime{$n};
18
19 if ( is_prime($n) ) {
20 $prime{$n} = 1;
21 return 1;
22 }
23 else {
24 $not_prime{$n} = 1;
25 return;
26 }
a43e2d4 @jtpalmer Problem 27, Perl 5.
authored
27 }
28
29 my $max = 0;
30 my $product;
76cae85 @jtpalmer Optimize problem 27.
authored
31 for my $a ( -999 .. 999 ) {
32 for my $b ( -999 .. 999 ) {
a43e2d4 @jtpalmer Problem 27, Perl 5.
authored
33 my $n = 0;
76cae85 @jtpalmer Optimize problem 27.
authored
34 while ( _is_prime( $n * $n + $a * $n + $b ) ) { $n++ }
35 if ( $n > $max ) {
36 $max = $n;
a43e2d4 @jtpalmer Problem 27, Perl 5.
authored
37 $product = $a * $b;
38 }
39 }
40 }
41
42 say $product;
Something went wrong with that request. Please try again.