NumSeq/Emirps.pm

filter: Statementstmt 98.3% 61 / 62 Branchbran 80.0% 8 / 10 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 97.1% 103 / 106 CC 9 SCAR 8.4
CC
9
Cov
96%
pred
4.1
next
4.0
_reverse_in_radix
3.0
CRAP 2.3
« NumSeq/SelfLengthCumulative.pm ↑ Summary NumSeq/SqrtEngel.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::Emirps;
19 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 $VERSION = 75;
24 2 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Math::NumSeq::Primes;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 @ISA = ('Math::NumSeq');
27 2 *_is_infinite = \&Math::NumSeq::_is_infinite;
28  
29  
30 # uncomment this to run the ### lines
31 #use Smart::Comments;
32  
33 # use constant name => Math::NumSeq::__('Emirps');
34 2 ▶use constant description => Math::NumSeq::__('Numbers which are primes forwards and backwards, eg. 157 because both 157 and 751 are primes. Palindromes like 131 are excluded. Default is decimal, or select a radix.');
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 use Math::NumSeq::Base::Digits
37 2 ▶ 'parameter_info_array'; # radix parameter
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 2 ▶use constant characteristic_increasing => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 # FIXME: find the first value in the sequence ... maybe save it
43 2 my @values_min;
44 2 $values_min[2] = 11; # binary 1011 reverse 1101 is decimal 13
45 2 $values_min[10] = 13; # reverse to 31
46 1 ▶sub values_min {
Subroutine values_min: called CC=1 SCAR=0.0
Pod documented
47 1 my ($self) = @_;
48 1 return $values_min[$self->{'radix'}];
49 }
50  
51 #------------------------------------------------------------------------------
52 # A006567 - decimal reversal is a prime and different
53 # A007500 - decimal reversal is a prime, so palindromes which are primes too
54 #
55 2 my @oeis_anum;
56 2 $oeis_anum[2] = 'A080790';
57 2 $oeis_anum[10] = 'A006567';
58 # OEIS-Catalogue: A080790 radix=2
59 # OEIS-Catalogue: A006567 radix=10
60 1 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
61 1 my ($self) = @_;
62 1 return $oeis_anum[$self->{'radix'}];
63 }
64  
65 #------------------------------------------------------------------------------
66  
67 4 ▶sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
68 4 my ($self) = @_;
69 4 $self->{'i'} = $self->i_start;
70 4 $self->{'primes_seq'} = Math::NumSeq::Primes->new;
71 }
72  
73 # ENHANCE-ME: The commented out code below took blocks of primes by radix
74 # powers and filtered. More memory but faster.
75 #
76 # ENHANCE-ME: No need to examine blocks where the high digit is even, or
77 # where it has a common factor with the radix.
78 #
79 100 ▶sub next {
Subroutine next: called CC=4 SCAR=13.9
Pod documented
80 100 my ($self) = @_;
81  
82 100 my $primes_seq = $self->{'primes_seq'};
83  
84 100 for (;;) {
85 392 ▶ (undef, my $prime) = $primes_seq->next
Branch: 1 branch
BranchTrueFalseTotal
my(undef, $prime) = $primes_seq->next or return 392 0 392
86 or return;
87 392 my $rev = _reverse_in_radix($prime,$self->{'radix'});
88  
89 ### consider: $prime
90 ### $rev
91  
92 392 ▶ if ($rev != $prime && $self->Math::NumSeq::Primes::pred($rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev != $prime and $self->Math::NumSeq::Primes::pred($rev)) 100 292 392
Condition100%3/3
$rev != $prime and $self->Math::NumSeq::Primes::pred($rev)
!ll&&!rl&&r
40252100
MC/DC100%2/2
$rev != $prime and $self->Math::NumSeq::Primes::pred($rev)
$rev != $prime $self->Math::NumSeq::Primes::pred($rev)
Truth table100%3/3
A and B
A= $rev != $primeB= $self->Math::NumSeq::Primes::pred($rev)
ABresult
0X0
100
111
93 ### yes ...
94 100 return ($self->{'i'}++, $prime);
95 }
96 }
97 }
98  
99 # ENHANCE-ME: are_all_prime() to look for small divisors in both values
100 # simultaneously, in case the reversal is even etc and easily excluded.
101 2007 ▶sub pred {
Subroutine pred: called CC=4 SCAR=14.0
Pod documented
102 2007 my ($self, $value) = @_;
103 2007 ▶ if (_is_infinite($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($value)) 0 2007 2007
104 0 return 0;
105 }
106 2007 my $rev = _reverse_in_radix($value,$self->{'radix'});
107 return ($rev != $value
108 && $self->Math::NumSeq::Primes::pred($value)
109 2007 ▶ && $self->Math::NumSeq::Primes::pred(_reverse_in_radix($value,$self->{'radix'})));
Condition100%6/6
$rev != $value && $self->Math::NumSeq::Primes::pred($value) && $self->Math::NumSeq::Primes::pred(_reverse_in_radix($value, $self->{'radix'}))
!ll&&!rl&&r
181011186
$rev != $value and $self->Math::NumSeq::Primes::pred($value)
!ll&&!rl&&r
971713197
MC/DC100%3/3
$rev != $value && $self->Math::NumSeq::Primes::pred($value) && $self->Math::NumSeq::Primes::pred(_reverse_in_radix($value, $self->{'radix'}))
$rev != $value $self->Math::NumSeq::Primes::pred($value) $self->Math::NumSeq::Primes::pred(_reverse_in_radix($value, $self->{'radix'}))
Truth table100%4/4
A and B && C
A= $rev != $valueB= $self->Math::NumSeq::Primes::pred($value)C= $self->Math::NumSeq::Primes::pred(_reverse_in_radix($value, $self->{'radix'}))
ABCresult
0XX0
10X0
1100
1111
110 }
111  
112 # return $n reversed in $radix
113 2599 ▶sub _reverse_in_radix {
Subroutine _reverse_in_radix: called CC=3 SCAR=11.0
114 2599 my ($n, $radix) = @_;
115  
116 2599 ▶ if ($radix == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 10) { } 2597 2 2599
117 2597 return scalar(reverse("$n"));
118 } else {
119 2 my $ret = $n*0; # inherit bignum 0
120 # ### _reverse_in_radix(): sprintf '%#X %d', $n, $n
121 2 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$n = int $n / $radix and do { do { $ret = $ret * $radix + $n % $radix } } 3 2 5
122 5 $ret = $ret * $radix + ($n % $radix);
123 } while ($n = int($n/$radix));
124 # ### ret: sprintf '%#X %d', $ret, $ret
125 2 return $ret;
126 }
127 }
128  
129 2 1;
130 __END__
« NumSeq/SelfLengthCumulative.pm ↑ Summary NumSeq/SqrtEngel.pm »