NumSeq/PrimeIndexPrimes.pm

filter: Statementstmt 100% 69 / 69 Branchbran 81.2% 13 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 97.0% 100 / 103 CC 12 SCAR 11.3
CC
12
Cov
96%
next
6.0
values_min
4.0
value_to_i_estimate
3.0
CRAP 3.1
« NumSeq/Beastly.pm ↑ Summary NumSeq/AllPrimeFactors.pm »
1 # Copyright 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  
19 # http://www.cs.uwaterloo.ca/journals/JIS/VOL12/Broughan/broughan16.pdf
20 # http://www.borve.org/primeness/FOP.html
21  
22  
23 package Math::NumSeq::PrimeIndexPrimes;
24 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 $VERSION = 75;
29 1 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 @ISA = ('Math::NumSeq');
31  
32 1 ▶use Math::NumSeq::Primes;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # uncomment this to run the ### lines
35 #use Smart::Comments;
36  
37 1 ▶use constant description => Math::NumSeq::__('The primes whose index in the list of primes is also a prime.');
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant characteristic_increasing => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
41 [ { name => 'level',
42 share_key => 'prime_index_primes_level',
43 display => Math::NumSeq::__('Level'),
44 type => 'integer',
45 default => 2,
46 minimum => 0,
47 width => 2,
48 description => Math::NumSeq::__('The level of prime-index repetition.'),
49 },
50 { name => 'level_type',
51 display => Math::NumSeq::__('Level Type'),
52 type => 'enum',
53 default => 'minimum',
54 choices => ['minimum','exact',
55 # 'maximum'
56 ],
57 },
58 1 ];
59  
60 #------------------------------------------------------------------------------
61 # cf A175247 primes at non-composite, including 1 as non-composite
62 # A007097 a(n+1) = a(n)'th prime, growing recurrence
63 # A058010 order of primeness diagonal
64  
65 1 my %oeis_anum
66 = (minimum => [
67 'A000027', # level=0 # integers 1 up
68 # OEIS-Other: A000027 level=0
69  
70 'A000040', # level=1 # all primes, F(p) >= 1
71 # OEIS-Other: A000040 level=1
72  
73 # OEIS-Catalogue array begin
74 'A006450', # # prime index primes F(p) >= 2
75 'A038580', # level=3 # F(p) >= 3
76 'A049090', # level=4 # F(p) > 3 is F(p) >= 4
77 'A049203', # level=5
78 'A049202', # level=6
79 'A057849', # level=7
80 'A057850', # level=8
81 'A057851', # level=9
82 'A057847', # level=10
83 'A058332', # level=11
84 'A093047', # level=12 # F(p)>11 is F(p)>=12
85 'A093046', # level=13
86 # OEIS-Catalogue array end
87 ],
88 exact => [
89 # OEIS-Catalogue array begin
90 'A018252', # level_type=exact level=0 # composites 1 up
91 'A007821', # level_type=exact level=1 # primes at compos ind
92 'A049078', # level_type=exact # F(p)=2
93 'A049079', # level_type=exact level=3
94 'A049080', # level_type=exact level=4
95 'A049081', # level_type=exact level=5
96 # OEIS-Catalogue array end
97 ],
98 );
99 6 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
100 6 my ($self) = @_;
101 6 return $oeis_anum{$self->{'level_type'}}->[$self->{'level'}];
102 }
103  
104 #------------------------------------------------------------------------------
105  
106 # First value of desired order is exactly that order, so same values_min()
107 # for level_type "minimum" or "exact".
108 #
109 6 ▶sub values_min {
Subroutine values_min: called CC=4 SCAR=14.0
Pod documented
110 6 my ($self) = @_;
111  
112 6 ▶ if (! defined $self->{'values_min'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"values_min"}) 6 0 6
113 6 my $seq = Math::NumSeq::Primes->new;
114 6 my $target = 1;
115 6 foreach (1 .. $self->{'level'}) {
116 6 my ($i, $prime);
117 6 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$i >= $target or do { do { () = $seq->next } } 6 0 6
118 6 ($i, $prime) = $seq->next;
119 } until ($i >= $target);
120 6 $target = $prime;
121 }
122 6 $self->{'values_min'} = $target;
123 }
124 6 return $self->{'values_min'};
125 }
126  
127 # primes, level=0
128 # 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
129 # 1 2 3 4 5 6 7 8 9 10 11 12 13
130 # ^ ^ ^ ^ ^ ^
131 # prime-index-primes, level=1
132 # 3, 5, 11, 17, 31, 41, 59, 67,
133 #
134 # prime-index-primes, level=2
135 # 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
136 # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
137 # ^ ^ ^ ^
138 # 5, 11, 31, 59,
139  
140 #
141 # exact level=1 A007821 2,7,13,19,23,29,37,43,47,53,61,71,73,79,
142 # exact level=2 A049078 3,17,41,67,83,109,157,191,211,241,283,353,
143 #
144  
145 18 ▶sub rewind {
Subroutine rewind: called CC=2 SCAR=6.9
Pod documented
146 18 my ($self) = @_;
147 18 $self->{'i'} = $self->i_start;
148 18 $self->{'target'} = 1;
149 18 $self->{'seqs'} = [ map { Math::NumSeq::Primes->new } 1 .. $self->{'level'} ];
150 18 ▶ if ($self->{'level_type'} ne 'minimum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"level_type"} ne "minimum") 9 9 18
151 9 $self->{'initial_seq'} = Math::NumSeq::Primes->new; # pop @$seqs;
152 9 $self->{'initial_prime'} = 0;
153 9 $self->{'target'} = 0;
154 }
155 }
156  
157 124 ▶sub next {
Subroutine next: called CC=6 SCAR=17.9
Pod documented
158 124 my ($self) = @_;
159 ### PrimeIndexPrimes next(): $self->{'i'}
160  
161 124 my $seqs = $self->{'seqs'};
162 124 my $target = $self->{'target'}++;
163  
164 124 ▶ if ($self->{'level_type'} eq 'maximum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'level_type'} eq 'maximum') { } 0 124 124
165  
166 } else {
167 124 ▶ if ($self->{'level_type'} eq 'exact') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"level_type"} eq "exact") 68 56 124
168 68 ▶ while ($target >= $self->{'initial_prime'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target >= $self->{"initial_prime"}) 50 68 118
169 50 (undef, $self->{'initial_prime'}) = $self->{'initial_seq'}->next;
170 50 $target = $self->{'target'}++;
171 }
172 }
173 124 foreach my $seq (@$seqs) {
174 ### $target
175 148 my ($i, $prime);
176 148 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$i >= $target or do { do { () = $seq->next } } 148 208 356
177 356 ($i, $prime) = $seq->next;
178 } until ($i >= $target);
179 148 $target = $prime;
180 }
181 }
182 124 return ($self->{'i'}++, $target);
183 }
184  
185 116 ▶sub value_to_i_estimate {
Subroutine value_to_i_estimate: called CC=3 SCAR=11.0
Pod documented
186 116 my ($self, $value) = @_;
187  
188 116 foreach (1 .. $self->{'level'}) {
189 128 $value = Math::NumSeq::Primes->value_to_i_estimate($value);
190 }
191 116 ▶ if ($self->{'level_type'} eq 'exact') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"level_type"} eq "exact") 61 55 116
192 61 $value = $value - Math::NumSeq::Primes->value_to_i_estimate($value);
193 }
194 116 return int($value);
195 }
196  
197 1 1;
198 __END__
« NumSeq/Beastly.pm ↑ Summary NumSeq/AllPrimeFactors.pm »