NumSeq/ReverseAddSteps.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 161 CC 8 SCAR 31.9
CC
8
Cov
0%
ith
72.0
oeis_anum
2.0
new
2.0
CRAP 24.4
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::ReverseAddSteps;
19 use 5.004;
20 use strict;
21  
22 use vars '$VERSION','@ISA';
23 $VERSION = 75;
24  
25 use Math::NumSeq;
26 use Math::NumSeq::Base::IterateIth;
27 @ISA = ('Math::NumSeq::Base::IterateIth',
28 'Math::NumSeq');
29 *_is_infinite = \&Math::NumSeq::_is_infinite;
30  
31 # uncomment this to run the ### lines
32 #use Devel::Comments;
33  
34  
35 # use constant name => Math::NumSeq::__('Reverse-Add Steps');
36 use constant description => Math::NumSeq::__('How many steps of reverse and add until a palindrome is reached (sometimes called the 196-algorithm).');
37 use constant i_start => 1;
38 use constant values_min => -1;
39 use constant characteristic_count => 1;
40 use constant characteristic_smaller => 1;
41 use constant characteristic_increasing => 0;
42  
43 use Math::NumSeq::Base::Digits
44 'parameter_info_array'; # radix parameter
45  
46 #------------------------------------------------------------------------------
47  
48 # http://oeis.org/index/Res#RAA reverse-adds
49 #
50 # cf A015976 - numbers needing 1 iteration to reach palindrome
51 # A065206 - 1 iteration to reach palindrome, excluding palindromes
52 # A015977 - 2 iterations to reach palindrome
53 # A015979 - 3 iterations to reach palindrome
54 # A033865 - the palindrome at which each n stops
55 #
56 # A023109 - first number requiring n iterations to palindrome
57 # suggesting where to make the hard limit ...
58 #
59 # A030547 - num steps, minimum 0, so palindromes value 1
60 #
61 # ~/OEIS/a058042.txt on reaching binary palindromes
62  
63 my @oeis_anum;
64 $oeis_anum[10] = 'A016016'; # steps to palindrome, or -1 if infinite
65 # OEIS-Catalogue: A016016
66  
67 sub oeis_anum {
68 my ($self) = @_;
69 return $oeis_anum[$self->{'radix'}];
70 }
71  
72  
73 #------------------------------------------------------------------------------
74  
75 use constant 1.02; # for leading underscore
76 use constant _LIMIT => 100;
77  
78 sub new {
79 my $self = shift->SUPER::new(@_);
80  
81 my $radix = $self->{'radix'};
82 my $limit = ~0;
83 my $uv_limit = 1;
84 while ($limit) {
85 $limit = int($limit/$radix);
86 $uv_limit *= $radix;
87 }
88 $self->{'uv_limit'} = $uv_limit;
89 ### $uv_limit
90  
91 return $self;
92 }
93  
94 sub ith {
95 my ($self, $k) = @_;
96 ### ReverseAddSteps ith(): $k
97  
98 if (_is_infinite($k) || $k < 0) {
99 return $k;
100 }
101  
102 my $radix = $self->{'radix'};
103 my $uv_limit = $self->{'uv_limit'};
104  
105 my $count = 0;
106 OUTER: for ( ; $count < _LIMIT; $count++) {
107 my @digits;
108 ### $count
109 ### k: "$k"
110  
111 if ($k >= $uv_limit && ! ref $k) {
112 $k = Math::NumSeq::_to_bigint($k);
113 }
114  
115 if (ref $k) {
116 ### big ...
117 my $d = $k->copy;
118 while ($d) {
119 ### d: "$d"
120 push @digits, $d % $radix;
121 $d->bdiv($radix);
122 }
123 @digits = (0) unless @digits;
124 ### big digits: join(',',@digits)
125  
126 for my $i (0 .. int(@digits/2)) {
127 if ($count == 0 || $digits[$i] != $digits[-1-$i]) {
128 ### not a palindrome ...
129  
130 foreach my $i (0 .. $#digits) {
131 $d->bmul($radix);
132 $d->badd($digits[$i]);
133 }
134 ### k: "$k"
135 ### d: "$d"
136 $k += $d;
137 ### sum now: "$k"
138 next OUTER;
139 }
140 }
141 } else {
142 my $d = $k;
143 while ($d) {
144 push @digits, $d % $radix;
145 $d = int($d/$radix);
146 }
147 @digits = (0) unless @digits;
148 ### small digits: join(',',@digits)
149  
150 for my $i (0 .. int(@digits/2)) {
151 if ($count == 0 || $digits[$i] != $digits[-1-$i]) {
152 ### not a palindrome ...
153  
154 foreach my $i (0 .. $#digits) {
155 $d *= $radix;
156 $d += $digits[$i];
157 }
158 ### k: "$k"
159 ### d: "$d"
160 $k += $d;
161 ### sum now: "$k"
162 next OUTER;
163 }
164 }
165 }
166 ### palindrome: $count
167 return $count;
168 }
169 ### limit reached, -1 ...
170 return -1;
171 }
172  
173 sub pred {
174 my ($self, $value) = @_;
175 return ($value == int($value) && $value >= -1);
176 }
177  
178 1;
179 __END__