-
Notifications
You must be signed in to change notification settings - Fork 33
/
prime_quadratic_polynomial_analyzer.pl
executable file
·55 lines (46 loc) · 1.95 KB
/
prime_quadratic_polynomial_analyzer.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 12 April 2015
# https://github.com/trizen
# Analyze the number of primes generated by each quadratic polynomial formula
use 5.010;
use strict;
use warnings;
use ntheory qw(is_prime);
my @formulas = (
sub { $_[0]**2 - $_[0] + 41 },
sub { $_[0]**2 - $_[0] + 111 },
sub { $_[0]**2 - $_[0] + 285 },
sub { $_[0]**2 - $_[0] + 171 },
sub { $_[0]**2 - $_[0] + 107 },
sub { $_[0]**2 - $_[0] + 101 },
sub { $_[0]**2 - $_[0] + 75 },
sub { $_[0]**2 - $_[0] + 315 },
sub { $_[0]**2 - $_[0] + 227 },
sub { $_[0]**2 - $_[0] + 621 },
);
my %top;
my $n = shift(@ARGV) // 100000;
foreach my $i (0 .. $#formulas) {
foreach my $j (1 .. $n) {
++$top{$i} if is_prime($formulas[$i]->($j));
}
}
foreach my $key (sort { $top{$b} <=> $top{$a} } keys %top) {
my $y = $formulas[$key]->(0);
my $f = sprintf("n^2 - n + %3d", $y);
printf "%13s: %56d (%5.2f%%)\n", $f, $top{$key}, $top{$key} / $n * 100;
}
__END__
# For n={1,100000}:
n^2 - n + 41: 31985 (31.99%)
n^2 - n + 107: 25162 (25.16%)
n^2 - n + 227: 24658 (24.66%)
n^2 - n + 101: 24549 (24.55%)
n^2 - n + 171: 8647 ( 8.65%)
n^2 - n + 111: 6838 ( 6.84%)
n^2 - n + 621: 3738 ( 3.74%)
n^2 - n + 315: 3305 ( 3.31%)
n^2 - n + 285: 2992 ( 2.99%)
n^2 - n + 75: 2933 ( 2.93%)