NumSeq/SpiroFibonacci.pm

filter: Statementstmt 76.0% 70 / 92 Branchbran 53.8% 14 / 26 Conditioncond 33.3% 6 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 75.0% 15 / 20 Podpod 71.4% 5 / 7 totaltotal 64.3% 112 / 174 CC 20 SCAR 19.4
CC
20
Cov
66%
values_max
20.0
_max
12.0
next
10.7
CRAP 7.0
« NumSeq/OEIS/Catalogue/Plugin/FractionDigits.pm ↑ Summary NumSeq/Runs.pm »
1 # Copyright 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 # cf
20 # A092360 Spiro-tribonacci numbers: a(n) = sum of three previous terms that are nearest when terms arranged in a spiral.
21 # A092369 Spiro-tetranacci numbers: a(n) = sum of four previous terms that are nearest when terms arranged in a spiral.
22 # A094769 Square spiral of sums of selected preceding terms, starting at 0 followed by 1 (a spiral Fibonacci-like sequence).
23  
24  
25 # math-image --values=SpiroFibonacci --output=numbers --size=78
26 #
27 # math-image --values=SpiroFibonacci,recurrence_type=absdiff --output=text --size=60x15
28  
29  
30 package Math::NumSeq::SpiroFibonacci;
31 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 $VERSION = 75;
36 1 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 @ISA = ('Math::NumSeq');
38 1 *_to_bigint = \&Math::NumSeq::_to_bigint;
39  
40 # uncomment this to run the ### lines
41 #use Smart::Comments;
42  
43  
44 # use constant name => Math::NumSeq::__('...');
45 1 ▶use constant description => Math::NumSeq::__('Spiro-Fibonacci recurrence.');
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant default_i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
49 [
50 # { name => 'spiral_type',
51 # type => 'enum',
52 # default => 'square',
53 # choices => ['square','hex'],
54 # },
55 { name => 'recurrence_type',
56 type => 'enum',
57 choices => ['additive','absdiff'],
58 default => 'additive',
59 },
60 { name => 'initial_0',
61 display => Math::NumSeq::__('Initial value 0'),
62 type => 'integer',
63 default => 0,
64 width => 3,
65 description => Math::NumSeq::__('Initial value ...'),
66 },
67 { name => 'initial_1',
68 display => Math::NumSeq::__('Initial value 1'),
69 type => 'integer',
70 default => 1,
71 width => 3,
72 description => Math::NumSeq::__('Initial value ...'),
73 },
74 1 ];
75  
76 0 ▶sub characteristic_integer {
Subroutine characteristic_integer: not called CC=2 SCAR=17.9
Pod undocumented
77 0 my ($self) = @_;
78 return (_is_integer($self->{'initial_0'})
79 0 ▶ && _is_integer($self->{'initial_1'}));
Condition0%0/3
_is_integer($self->{'initial_0'}) && _is_integer($self->{'initial_1'})
!ll&&!rl&&r
000
MC/DC0%0/2
_is_integer($self->{'initial_0'}) && _is_integer($self->{'initial_1'})
_is_integer($self->{'initial_0'}) _is_integer($self->{'initial_1'})
Truth table0%0/3
A && B
A= _is_integer($self->{'initial_0'})B= _is_integer($self->{'initial_1'})
ABresult
0X0
100
111
80 }
81 0 ▶sub characteristic_smaller {
Subroutine characteristic_smaller: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my ($self) = @_;
83 0 return ($self->{'recurrence_type'} eq 'absdiff');
84 }
85 0 ▶sub _is_integer {
Subroutine _is_integer: not called CC=1 SCAR=6.9
86 0 my ($n) = @_;
87 0 return ($n == int($n));
88 }
89  
90 # FIXME: absdiff range related to least common multiple, maybe
91 1 ▶sub values_min {
Subroutine values_min: called CC=3 SCAR=13.3
Pod documented
92 1 my ($self) = @_;
93 1 ▶ if ($self->{'initial_0'} >= 0 && $self->{'initial_1'} >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"initial_0"} >= 0 and $self->{"initial_1"} >= 0) 1 0 1
Condition33%1/3
$self->{"initial_0"} >= 0 and $self->{"initial_1"} >= 0
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"initial_0"} >= 0 and $self->{"initial_1"} >= 0
$self->{"initial_0"} >= 0 $self->{"initial_1"} >= 0
Truth table33%1/3
A and B
A= $self->{"initial_0"} >= 0B= $self->{"initial_1"} >= 0
ABresult
0X0
100
111
94 1 return _min (0, $self->{'initial_0'}, $self->{'initial_1'});
95 }
96 0 return undef;
97 }
98 0 ▶sub values_max {
Subroutine values_max: not called CC=4 SCAR=30.0
Pod documented
99 0 my ($self) = @_;
100 0 ▶ if ($self->{'recurrence_type'} eq 'absdiff') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'recurrence_type'} eq 'absdiff') { } 0 0 0
101 return _max (abs($self->{'initial_0'}),
102 0 abs($self->{'initial_1'}));
103 } else {
104 0 ▶ if ($self->{'initial_0'} <= 0 && $self->{'initial_1'} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'initial_0'} <= 0 and $self->{'initial_1'} <= 0) { } 0 0 0
Condition0%0/3
$self->{"initial_0"} <= 0 and $self->{"initial_1"} <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"initial_0"} <= 0 and $self->{"initial_1"} <= 0
$self->{"initial_0"} <= 0 $self->{"initial_1"} <= 0
Truth table0%0/3
A and B
A= $self->{"initial_0"} <= 0B= $self->{"initial_1"} <= 0
ABresult
0X0
100
111
105 return _max ($self->{'initial_0'},
106 0 $self->{'initial_1'});
107 } else {
108 0 return undef;
109 }
110 }
111 }
112 1 ▶sub _min {
Subroutine _min: called CC=3 SCAR=11.2
113 1 my $ret = shift;
114 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
115 2 my $next = shift;
116 2 ▶ if ($ret > $next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret > $next) 0 2 2
117 0 $ret = $next;
118 }
119 }
120 1 return $ret;
121 }
122 0 ▶sub _max {
Subroutine _max: not called CC=3 SCAR=24.8
123 0 my $ret = shift;
124 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
125 0 my $next = shift;
126 0 ▶ if ($next > $ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next > $ret) 0 0 0
127 0 $ret = $next;
128 }
129 }
130 0 return $ret;
131 }
132  
133  
134 #------------------------------------------------------------------------------
135  
136 # cf A079422 cumulative absdiff up to n^2
137 # A094925 hexagonal 1,1 three around a(n-1)
138 # A094926 hexagonal 0,1 three around a(n-1)
139  
140 1 my %oeis_anum = ('additive,0,1' => 'A078510',
141 # OEIS-Catalogue: A078510
142  
143 'absdiff,0,1' => 'A079421',
144 # OEIS-Catalogue: A079421 recurrence_type=absdiff
145  
146 # all zeros if initial_0 == initial_1 == 0
147 'additive,0,0' => 'A000004',
148 'absdiff,0,0' => 'A000004',
149 # OEIS-Other: A000004 initial_1=0
150 # OEIS-Other: A000004 initial_1=0 recurrence_type=absdiff
151 );
152  
153 1 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
154 1 my ($self) = @_;
155 return $oeis_anum{join(',',
156 1 @{$self}{ # hash slice
157 1 'recurrence_type',
158 'initial_0',
159 'initial_1'})};
160 }
161  
162  
163 #------------------------------------------------------------------------------
164  
165 1 ▶use constant 1.02;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 1 ▶use constant _UV_LIMIT => (~0 >> 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
167 1 ▶use constant _IV_NEG_LIMIT => - (_UV_LIMIT >> 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 # my %spiral_type = (square => { num_sides => 2,
170 # side_longer => -1,
171 # },
172 # hex => { num_sides => 6,
173 # side_longer => 1,
174 # },
175 # );
176  
177 3 ▶sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
178 3 my ($self) = @_;
179 3 $self->{'i'} = $self->i_start;
180 3 $self->{'queue'} = [$self->{'initial_0'}, $self->{'initial_1'}];
181 3 $self->{'num_sides'} = 2;
182 3 $self->{'side_len'} = 1;
183 3 $self->{'side'} = $self->{'num_sides'};
184 3 $self->{'count'} = 2;
185 }
186  
187 24 ▶sub next {
Subroutine next: called CC=10 SCAR=23.7
Pod documented
188 24 my ($self) = @_;
189 ### next(): "i=$self->{'i'} side_len=$self->{'side_len'} side=$self->{'side'} count=$self->{'count'}"
190  
191 24 my $queue = $self->{'queue'};
192 24 my $i = $self->{'i'}++;
193 24 ▶ if ($i < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 2) 4 20 24
194 ### initial queue ...
195 4 return ($i, $queue->[$i]);
196 }
197  
198 20 my $value;
199 20 ▶ if ($self->{'recurrence_type'} eq 'absdiff') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'recurrence_type'} eq 'absdiff') { } 0 20 20
200 0 $value = abs($queue->[-1] - $queue->[0]);
201 } else {
202 20 $value = $queue->[-1] + $queue->[0];
203 }
204 20 ▶ if (! ref $value && ($value >= _UV_LIMIT || $value <= _IV_NEG_LIMIT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value and $value >= '9223372036854775807' || $value <= '-4611686018427387903') 0 20 20
Condition33%2/6
not ref $value and $value >= '9223372036854775807' || $value <= '-4611686018427387903'
!ll&&!rl&&r
0200
$value >= '9223372036854775807' or $value <= '-4611686018427387903'
l!l&&r!l&&!r
0020
MC/DC0%0/3
not ref $value and $value >= '9223372036854775807' || $value <= '-4611686018427387903'
not ref $value $value >= '9223372036854775807' $value <= '-4611686018427387903'
Truth table25%1/4
not(A) and B or C
A= not ref $valueB= $value >= '9223372036854775807'C= $value <= '-4611686018427387903'
ABCresult
0XX0
1000
11X1
1011
205 0 $queue->[-1] = _to_bigint($queue->[-1]);
206 }
207  
208 20 push @$queue, $value;
209 20 my $count = --$self->{'count'};
210 ### count down to: $count
211  
212 20 ▶ if ($count > 0 && $self->{'side_len'} >= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count > 0 and $self->{'side_len'} >= 2) { } 4 16 20
elsif ($count < 0) { } 6 10 16
Condition100%3/3
$count > 0 and $self->{"side_len"} >= 2
!ll&&!rl&&r
1424
MC/DC100%2/2
$count > 0 and $self->{"side_len"} >= 2
$count > 0 $self->{"side_len"} >= 2
Truth table100%3/3
A and B
A= $count > 0B= $self->{"side_len"} >= 2
ABresult
0X0
100
111
213 4 shift @$queue;
214 } elsif ($count < 0) {
215 ### end of side, next side ...
216 6 ▶ if (--$self->{'side'} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{"side"} <= 0) 2 4 6
217 ### end of sides, next loop ...
218 2 $self->{'side'} = $self->{'num_sides'};
219 2 $self->{'side_len'}++;
220 }
221 6 $self->{'count'} = $self->{'side_len'};
222 } else {
223 ### count==0 corner ...
224 }
225 20 return ($i, $value);
226 }
227  
228 # sub new {
229 # ### SpiroFibonacci new(): @_
230 # my $self = shift->SUPER::new(@_);
231 # require Math::PlanePath::SquareSpiral;
232 # $self->{'path'} = Math::PlanePath::SquareSpiral->new;
233 # $self->{'cache'} = [];
234 # $self->{'cache'}->[1] = $self->{'initial_0'};
235 # $self->{'cache'}->[2] = $self->{'initial_1'};
236 # return $self;
237 # }
238 #
239 # sub ith {
240 # my ($self, $i) = @_;
241 # ### SpiroFibonacci ith(): $i
242 #
243 # if (_is_infinite($i)) {
244 # return undef;
245 # }
246 #
247 # if ($i <= 0) {
248 # return undef;
249 # }
250 # my $orig_i = $i;
251 #
252 # my $cache = $self->{'cache'};
253 # my $path = $self->{'path'};
254 # my @pending = ($i);
255 # my $total = 0;
256 #
257 # while (@pending) {
258 # ### @pending
259 # $i = pop @pending;
260 # if (defined $cache->[$i]) {
261 # if ($self->{'recurrence_type'} eq 'absdiff') {
262 # $total ^= $cache->[$i];
263 # } else {
264 # $total += $cache->[$i];
265 # }
266 # } else {
267 # my ($x,$y) = $path->n_to_xy($i);
268 # my $pi = 0;
269 # foreach my $dxdy ([0,1],[0,-1],
270 # [1,0],[-1,0]) {
271 # my ($dx,$dy) = @$dxdy;
272 # my $n = $path->xy_to_n($x+$dx,$y+$dy);
273 # if ($n < $i-1 && $n > $pi) {
274 # ### straight at: "i=$i,x=$x,y=$y dx=$dx dy=$dy pi=$n"
275 # $pi = $n;
276 # }
277 # }
278 # if (! $pi) {
279 # foreach my $dxdy ([1,1],[1,-1],
280 # [-1,1],[-1,-1]) {
281 # my ($dx,$dy) = @$dxdy;
282 # my $n = $path->xy_to_n($x+$dx,$y+$dy);
283 # if ($n < $i-1 && $n > $pi) {
284 # ### diagonal at: "i=$i,x=$x,y=$y dx=$dx dy=$dy pi=$n"
285 # $pi = $n;
286 # }
287 # }
288 # }
289 # if (@pending > 100000) { die; }
290 # push @pending, $pi;
291 # push @pending, $i-1;
292 # }
293 # }
294 # $self->{'cache'}->[$orig_i] = $total;
295 #
296 # ### $total
297 # return $total;
298 # }
299  
300 1 1;
301 __END__
« NumSeq/OEIS/Catalogue/Plugin/FractionDigits.pm ↑ Summary NumSeq/Runs.pm »