NumSeq/TwinPrimes.pm

filter: Statementstmt 100% 82 / 82 Branchbran 87.5% 14 / 16 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 18 / 18 Podpod 100% 6 / 6 totaltotal 98.5% 139 / 141 CC 13 SCAR 12.4
CC
13
Cov
98%
pred
8.0
next
4.0
value_to_i_estimate
3.0
CRAP 3.5
« NumSeq/Base/Cache.pm ↑ Summary NumSeq/ReRound.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde
2  
3 # This file is part of Math-NumSeq.
4 #
5 # Math-NumSeq is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-NumSeq is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Math::NumSeq::TwinPrimes;
19 3 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 $VERSION = 75;
24  
25 3 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Math::NumSeq::Primes;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 @ISA = ('Math::NumSeq');
28  
29 # uncomment this to run the ### lines
30 #use Devel::Comments;
31  
32  
33 # use constant name => Math::NumSeq::__('Twin Primes');
34 3 ▶use constant description => Math::NumSeq::__('The twin primes, 3, 5, 7, 11, 13, being integers where both K and K+2 are primes.');
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use constant i_start => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use constant characteristic_increasing => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
39 [
40 { name => 'pairs',
41 display => Math::NumSeq::__('Pairs'),
42 type => 'enum',
43 default => 'first',
44 choices => ['first','second','both','average'],
45 choices_display => [Math::NumSeq::__('First'),
46 Math::NumSeq::__('Second'),
47 Math::NumSeq::__('Both'),
48 Math::NumSeq::__('Average')],
49 description => Math::NumSeq::__('Which of a pair of values to show.'),
50 },
51 3 ];
52  
53 3 my %values_min = (first => 3,
54 second => 5,
55 both => 3,
56 average => 4);
57 3 ▶sub values_min {
Subroutine values_min: called CC=1 SCAR=0.0
Pod documented
58 3 my ($self) = @_;
59 3 return $values_min{$self->{'pairs'}};
60 }
61  
62 #------------------------------------------------------------------------------
63 # cf A077800 - both, with repetition, so 3,5, 5,7, 11,13, ...
64 # A040040 - average/2 since the average is always even
65 # A054735 - sum twin primes (OFFSET=1)
66 # A111046 - p^2 - q^2
67 # A167777 - even "isolated" numbers, 2 plus twin primes average
68 # A129297 - m s.t. m^2-1 no no divisors 1<d<m-1, twin average plus 0..3
69 #
70 # A067774 - primes where p+2 not prime
71 # A063637 - primes where p+2 is a semiprime
72 #
73 # A048598 - cumulative total twin primes
74 # A100923 - characteristic 0,1 according to 6n+/-1 both primes
75 # ie. twin prime 6n-1,6n+1
76 #
77 3 my %oeis_anum = (
78 first => 'A001359',
79 # OEIS-Catalogue: A001359 pairs=first
80  
81 second => 'A006512',
82 # OEIS-Catalogue: A006512 pairs=second
83  
84 both => 'A001097', # both, without repetition
85 # OEIS-Catalogue: A001097 pairs=both
86  
87 average => 'A014574', # average
88 # OEIS-Catalogue: A014574 pairs=average
89 );
90 7 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
91 7 my ($self) = @_;
92 7 return $oeis_anum{$self->{'pairs'}};
93 }
94  
95 #------------------------------------------------------------------------------
96  
97 3 my %pairs_add = (first => 0,
98 average => 1,
99 second => 2,
100 both => 0);
101  
102 15 ▶sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
103 15 my ($self) = @_;
104 ### TwinPrimes rewind() ...
105  
106 15 $self->{'i'} = $self->i_start;
107 15 my $primes_seq = $self->{'primes_seq'} = Math::NumSeq::Primes->new;
108 15 $self->{'twin_both'} = 0;
109 15 (undef, $self->{'twin_prev'}) = $primes_seq->next;
110 }
111  
112 67 ▶sub next {
Subroutine next: called CC=4 SCAR=13.9
Pod documented
113 67 my ($self) = @_;
114 ### TwinPrimes next(): "i=$self->{'i'} prev=$self->{'twin_prev'}"
115  
116 67 my $prev = $self->{'twin_prev'};
117 67 my $primes_seq = $self->{'primes_seq'};
118  
119 67 for (;;) {
120 113 ▶ (undef, my $prime) = $primes_seq->next
Branch: 1 branch
BranchTrueFalseTotal
my(undef, $prime) = $primes_seq->next or return 113 0 113
121 or return;
122  
123 113 ▶ if ($prime == $prev + 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prime == $prev + 2) { } 55 58 113
elsif ($self->{'twin_both'}) { } 12 46 58
124 55 my $pairs = $self->{'pairs'};
125 55 $self->{'twin_prev'} = $prime;
126 55 $self->{'twin_both'} = ($pairs eq 'both');
127 55 return ($self->{'i'}++, $prev + $pairs_add{$pairs});
128  
129 } elsif ($self->{'twin_both'}) {
130 12 $self->{'twin_prev'} = $prime;
131 12 $self->{'twin_both'} = 0;
132 12 return ($self->{'i'}++, $prev);
133 }
134 46 $prev = $prime;
135 }
136 }
137  
138  
139 # ENHANCE-ME: are_all_prime() to look for small divisors in both values
140 # simultaneously, in case the reversal is even etc and easily excluded.
141 #
142 3 my %pairs_other = (first => 2,
143 average => 1,
144 second => 0);
145 3 my %pairs_mod = (first => 5,
146 average => 0,
147 second => 1);
148 182 ▶sub pred {
Subroutine pred: called CC=8 SCAR=20.8
Pod documented
149 182 my ($self, $value) = @_;
150 182 ▶ if ((my $pairs = $self->{'pairs'}) eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pairs = $self->{'pairs'}) eq 'both') { } 66 116 182
151 66 ▶ return ($self->Math::NumSeq::Primes::pred ($value)
Condition100%6/6
$self->Math::NumSeq::Primes::pred($value) && ($self->Math::NumSeq::Primes::pred($value + 2) || $self->Math::NumSeq::Primes::pred($value - 2))
!ll&&!rl&&r
47118
$self->Math::NumSeq::Primes::pred($value + 2) or $self->Math::NumSeq::Primes::pred($value - 2)
l!l&&r!l&&!r
1081
MC/DC100%3/3
$self->Math::NumSeq::Primes::pred($value) && ($self->Math::NumSeq::Primes::pred($value + 2) || $self->Math::NumSeq::Primes::pred($value - 2))
$self->Math::NumSeq::Primes::pred($value) $self->Math::NumSeq::Primes::pred($value + 2) $self->Math::NumSeq::Primes::pred($value - 2)
Truth table100%4/4
A && B or C
A= $self->Math::NumSeq::Primes::pred($value)B= $self->Math::NumSeq::Primes::pred($value + 2)C= $self->Math::NumSeq::Primes::pred($value - 2)
ABCresult
0XX0
1000
11X1
1011
152 && ($self->Math::NumSeq::Primes::pred ($value + 2)
153 || $self->Math::NumSeq::Primes::pred ($value - 2)));
154 } else {
155 # pairs are always 3n-1,3n+1 since otherwise one of them would be a 3n
156 # and also both odd so 6n-1,6n+1
157 116 ▶ if (my $mod = $pairs_mod{$pairs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mod = $pairs_mod{$pairs}) 116 0 116
158 116 ▶ if ($value >= 6 && ($value % 6) != $mod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value >= 6 and $value % 6 != $mod) 82 34 116
Condition100%3/3
$value >= 6 and $value % 6 != $mod
!ll&&!rl&&r
112382
MC/DC100%2/2
$value >= 6 and $value % 6 != $mod
$value >= 6 $value % 6 != $mod
Truth table100%3/3
A and B
A= $value >= 6B= $value % 6 != $mod
ABresult
0X0
100
111
159 82 return 0;
160 }
161 }
162 return ($self->Math::NumSeq::Primes::pred ($value - $pairs_add{$pairs})
163 34 ▶ && $self->Math::NumSeq::Primes::pred ($value + $pairs_other{$pairs}));
Condition100%3/3
$self->Math::NumSeq::Primes::pred($value - $pairs_add{$pairs}) && $self->Math::NumSeq::Primes::pred($value + $pairs_other{$pairs})
!ll&&!rl&&r
12220
MC/DC100%2/2
$self->Math::NumSeq::Primes::pred($value - $pairs_add{$pairs}) && $self->Math::NumSeq::Primes::pred($value + $pairs_other{$pairs})
$self->Math::NumSeq::Primes::pred($value - $pairs_add{$pairs}) $self->Math::NumSeq::Primes::pred($value + $pairs_other{$pairs})
Truth table100%3/3
A && B
A= $self->Math::NumSeq::Primes::pred($value - $pairs_add{$pairs})B= $self->Math::NumSeq::Primes::pred($value + $pairs_other{$pairs})
ABresult
0X0
100
111
164 }
165 }
166  
167 # Hardy and Littlewood conjecture, then
168 # pi2(x) -> x / (ln x)^2
169 #
170 # Brun upper bound
171 # pi2(x) <= const * C2 * x/(ln x)^2 * (1 + O(ln ln x / ln x))
172 # with const < 68/9 ~= 7.55
173 #
174 # x
175 # pi2(x) ~ 2*C2 * integral 1/(ln x)^2 dx
176 # 2
177 #
178 # cf pi2(x) ~ 2*C2 * pi(x)^2 / x
179 #
180 # integral 1/(ln x)^2 = li(x) - x/ln(x)
181 # li(x) = int 1/ln(x)
182 #
183  
184 # C2 = product (1 - 1/(p-1)^2) for all primes p>2
185 #
186 3 ▶use constant 1.02 _TWIN_PRIME_CONSTANT => 0.6601618158;
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 3 ▶use Math::NumSeq::Fibonacci;
Subroutine BEGIN: called CC=1 SCAR=0.0
189 3 *_blog2_estimate = \&Math::NumSeq::Fibonacci::_blog2_estimate;
190  
191 108 ▶sub value_to_i_estimate {
Subroutine value_to_i_estimate: called CC=3 SCAR=11.0
Pod documented
192 108 my ($self, $value) = @_;
193 ### value_to_i_estimate(): $value
194  
195 108 ▶ if ($value < 2) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 2) 29 79 108
196  
197 79 $value = int($value);
198 79 ▶ if (defined (my $blog2 = _blog2_estimate($value))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $blog2 = _blog2_estimate($value))) 4 75 79
199 # est = v/(log(v)^2) * 2*tpc
200 # log2(v) = log(v)/log(2)
201 # est = v/((log2(v)^2 * log(2)^2)) * 2*tpc
202 # = v/(log2(v)^2) * 2*tpc/(log(2)^2)
203 # ~= v/(log2(v)^2) * 11/4
204 # using 11/4 as an approximation to 2*tpc/(log(2)^2) to stay in BigInt
205 #
206 ### $blog2
207 ### num: $value*13
208 ### den: 9 * $blog2
209 4 return ($value * 11) / (4 * $blog2 * $blog2);
210 }
211  
212 75 my $log = log($value);
213 75 return int($value / ($log*$log) * (2 * _TWIN_PRIME_CONSTANT));
214 }
215 3 1;
216 __END__
« NumSeq/Base/Cache.pm ↑ Summary NumSeq/ReRound.pm »