NumSeq/Palindromes.pm

filter: Statementstmt 98.8% 82 / 83 Branchbran 92.8% 13 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 96.6% 117 / 121 CC 10 SCAR 11.0
CC
10
Cov
97%
ith
6.0
pred
5.0
BEGIN
1.0
CRAP 3.0
« NumSeq/DigitProduct.pm ↑ Summary NumSeq/DigitSumModulo.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019 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 # cf A206913 next binary palindrome <= n value_floor
20 # A206914 next binary palindrome >= n value_ceil
21 # A206920 sum first n binary palindromes
22  
23  
24 package Math::NumSeq::Palindromes;
25 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 $VERSION = 74;
31  
32 1 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Math::NumSeq::Base::IterateIth;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 @ISA = ('Math::NumSeq::Base::IterateIth',
35 'Math::NumSeq');
36 1 *_is_infinite = \&Math::NumSeq::_is_infinite;
37  
38 1 ▶use Math::NumSeq::Repdigits;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 *_digit_split_lowtohigh = \&Math::NumSeq::Repdigits::_digit_split_lowtohigh;
40  
41 # uncomment this to run the ### lines
42 #use Smart::Comments;
43  
44  
45 # use constant name => Math::NumSeq::__('Palindromes');
46 1 ▶use constant description => Math::NumSeq::__('Numbers which are "palindromes" reading the same backwards or forwards, like 153351. Default is decimal, or select a radix.');
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant i_start => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant values_min => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant characteristic_increasing => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 use Math::NumSeq::Base::Digits
53 1 ▶ 'parameter_info_array'; # radix parameter
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55  
56 #------------------------------------------------------------------------------
57 # cf A002385 - prime palindromes
58 # A029732 - prime palindromes in base 16, written in base 10
59 # A110784 - palindrom and digits in ascending order
60 # A029731 - palindromes in both decimal and hexadecimal
61 # A029733 - n where n^2 hex palindrome
62 # A029734 - squares which are hex palindromes
63 # A016038 - not palindrome in any base, up to n-2
64 # A057891 - not a binary palindrome, including if trailing 0-bits stripped
65  
66 1 my @oeis_anum = (
67 # OEIS-Catalogue array begin
68 undef, # 0
69 undef, # 1
70 'A006995', # radix=2
71 'A014190', # radix=3
72 'A014192', # radix=4
73 'A029952', # radix=5
74 'A029953', # radix=6
75 'A029954', # radix=7
76 'A029803', # radix=8
77 'A029955', # radix=9
78 'A002113', #
79 'A029956', # radix=11
80 'A029957', # radix=12
81 'A029958', # radix=13
82 'A029959', # radix=14
83 'A029960', # radix=15
84 'A029730', # radix=16
85 # OEIS-Catalogue array end
86 );
87 sub oeis_anum {
88 9 ▶ my ($self) = @_;
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
89 9 return $oeis_anum[$self->{'radix'}];
90 }
91  
92 #------------------------------------------------------------------------------
93  
94 sub ith {
95 13113 ▶ my ($self, $i) = @_;
Subroutine ith: called CC=6 SCAR=17.9
Pod documented
96 ### Palindrome ith(): $i
97  
98 13113 ▶ if (_is_infinite($i)) { # don't loop forever if $value is +/-infinity
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($i)) 0 13113 13113
99 0 return undef;
100 }
101  
102 13113 my $radix = $self->{'radix'};
103  
104 13113 ▶ if ($i < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 1) 36 13077 13113
105 36 return 0;
106 }
107 13077 $i -= 2;
108  
109 13077 my $digits = 1;
110 13077 my $limit = $radix-1;
111 13077 my $add = 1;
112 13077 my $ret;
113 13077 for (;;) {
114 30253 ▶ if ($i < $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $limit) 9699 20554 30253
115 ### first, no low ...
116 9699 $i += $add;
117 9699 $ret = int($i / $radix);
118 9699 last;
119 }
120 20554 $i -= $limit;
121 20554 ▶ if ($i < $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $limit) 3378 17176 20554
122 ### second ...
123 3378 $i += $add;
124 3378 $ret = $i;
125 3378 last;
126 }
127 17176 $i -= $limit;
128 17176 $limit *= $radix;
129 17176 $add *= $radix;
130 17176 $digits++;
131 }
132 ### $limit
133 ### $add
134 ### $i
135 ### $digits
136 ### push under: $ret
137 13077 ▶ while ($digits--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digits--) 30253 13077 43330
138 30253 $ret = $ret * $radix + ($i % $radix);
139 30253 $i = int($i / $radix);
140 }
141 ### $ret
142 13077 return $ret;
143 }
144  
145 sub pred {
146 12891 ▶ my ($self, $value) = @_;
Subroutine pred: called CC=5 SCAR=16.1
Pod documented
147  
148 12891 ▶ if (_is_infinite($value) # don't loop forever if $value is +/-infinity
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($value) or $value != int $value) 6153 6738 12891
Condition66%2/3
_is_infinite($value) or $value != int $value
l!l&&r!l&&!r
061536738
MC/DC50%1/2
_is_infinite($value) or $value != int $value
_is_infinite($value) $value != int $value
Truth table66%2/3
A or B
A= _is_infinite($value)B= $value != int $value
ABresult
1X1
011
000
149 || $value != int($value)) {
150 6153 return 0;
151 }
152  
153 6738 my @digits = _digit_split_lowtohigh($value, $self->{'radix'});
154 6738 for my $i (0 .. int(@digits/2)-1) {
155 8433 ▶ if ($digits[$i] != $digits[-1-$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digits[$i] != $digits[-1 - $i]) 5596 2837 8433
156 5596 return 0;
157 }
158 }
159 1142 return 1;
160 }
161  
162 1 1;
163 __END__
« NumSeq/DigitProduct.pm ↑ Summary NumSeq/DigitSumModulo.pm »