NumSeq/CullenNumbers.pm

filter: Statementstmt 88.8% 72 / 81 Branchbran 68.7% 11 / 16 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 88.2% 15 / 17 Podpod 100% 4 / 4 totaltotal 85.9% 110 / 128 CC 9 SCAR 11.7
CC
9
Cov
85%
pred
7.0
_UNTESTED__seek_to_i
6.0
next
2.0
CRAP 3.2
« NumSeq/GolayRudinShapiro.pm ↑ Summary NumSeq/Primes.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 package Math::NumSeq::CullenNumbers;
19 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 $VERSION = 75;
24  
25 1 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 @ISA = ('Math::NumSeq');
27 1 *_is_infinite = \&Math::NumSeq::_is_infinite;
28  
29 # uncomment this to run the ### lines
30 #use Smart::Comments;
31  
32  
33 # use constant name => Math::NumSeq::__('Cullen Numbers');
34 1 ▶use constant description => Math::NumSeq::__('Cullen numbers n*2^n+1.');
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant values_min => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant characteristic_increasing => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use constant oeis_anum => 'A002064';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # pow*i+1
42 1 my $uv_i_limit = do {
43 1 my $max = ~0;
44 1 my $limit = 1;
45  
46 1 ▶ for (my $i = 1; $i < 1000; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 1000) 59 0 59
47 59 ▶ if ($i <= ($max-1) >> $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max - 1 >> $i) { } 58 1 59
48 58 $limit = $i;
49 } else {
50 1 last;
51 }
52 }
53  
54 ### max : $max
55 ### cullen: (1<<$limit)*$limit+1
56 ### assert: $limit*(1<<$limit)+1 <= $max
57  
58 1 $limit
59 };
60 ### $uv_i_limit
61  
62 # or maybe ...
63 # (i+1)*2^(i+1) + 1
64 # = 2*(i+1)*2^i + 1
65 # = (2i+2)*2^i + 1
66 # = 2i*2^i + 2*2^i + 1
67 # = 2(i*2^i + 1) -2 + 2*2^i + 1
68 # = 2(i*2^i + 1) + 2*2^i - 1
69  
70 3 ▶sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
71 3 my ($self) = @_;
72 3 my $i = $self->{'i'} = $self->i_start;
73 3 $self->{'power'} = 2 ** $i;
74 }
75 0 ▶sub _UNTESTED__seek_to_i {
Subroutine _UNTESTED__seek_to_i: not called CC=2 SCAR=17.9
76 0 my ($self, $i) = @_;
77 0 $self->{'i'} = $i;
78 0 ▶ if ($i >= $uv_i_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $uv_i_limit) 0 0 0
79 0 $i = Math::NumSeq::_to_bigint($i);
80 }
81 0 $self->{'power'} = 2 ** $i;
82 }
83 0 ▶sub _UNTESTED__seek_to_value {
Subroutine _UNTESTED__seek_to_value: not called CC=1 SCAR=6.9
84 0 my ($self, $value) = @_;
85 0 $self->seek_to_i($self->value_to_i_ceil($value));
86 }
87 20 ▶sub next {
Subroutine next: called CC=2 SCAR=7.1
Pod documented
88 20 my ($self) = @_;
89 20 my $i = $self->{'i'}++;
90 20 ▶ if ($i == $uv_i_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $uv_i_limit) 0 20 20
91 0 $self->{'power'} = Math::NumSeq::_to_bigint($self->{'power'});
92 }
93 20 my $value = $self->{'power'}*$i + 1;
94 20 $self->{'power'} *= 2;
95 20 return ($i, $value);
96 }
97  
98 33 ▶sub ith {
Subroutine ith: called CC=1 SCAR=0.0
Pod documented
99 33 my ($self, $i) = @_;
100 33 return $i * 2**$i + 1;
101 }
102 1803 ▶sub pred {
Subroutine pred: called CC=7 SCAR=19.5
Pod documented
103 1803 my ($self, $value) = @_;
104 ### CullenNumbers pred(): $value
105  
106 {
107 1803 my $int = int($value);
108 1803 ▶ if ($value != $int) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value != $int) 896 907 1803
109 907 $value = $int;
110 }
111 907 ▶ unless ($value >= 1
Branch: 1 branch
BranchTrueFalseTotal
unless ($value >= 1 and $value % 2 == 1) 448 459 907
Condition66%2/3
$value >= 1 and $value % 2 == 1
!ll&&!rl&&r
0448459
MC/DC50%1/2
$value >= 1 and $value % 2 == 1
$value >= 1 $value % 2 == 1
Truth table66%2/3
A and B
A= $value >= 1B= $value % 2 == 1
ABresult
0X0
100
111
112 && ($value % 2) == 1) {
113 448 return 0;
114 }
115  
116 459 $value -= 1; # now seeking $value == $exp * 2**$exp
117  
118 459 ▶ if (_is_infinite($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($value)) 0 459 459
119 0 return 0;
120 }
121  
122 459 my $exp = 0;
123 459 for (;;) {
124 1384 ▶ if ($value <= $exp || $value % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= $exp or $value % 2) 459 925 1384
Condition100%3/3
$value <= $exp or $value % 2
l!l&&r!l&&!r
30429925
MC/DC100%2/2
$value <= $exp or $value % 2
$value <= $exp $value % 2
Truth table100%3/3
A or B
A= $value <= $expB= $value % 2
ABresult
1X1
011
000
125 459 return ($value == $exp);
126 }
127 925 $value = int($value/2);
128 925 $exp++;
129 }
130 }
131  
132 1 ▶use Math::NumSeq::WoodallNumbers;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 1 *value_to_i_estimate = \&Math::NumSeq::WoodallNumbers::value_to_i_estimate;
134  
135 1 1;
136 __END__
« NumSeq/GolayRudinShapiro.pm ↑ Summary NumSeq/Primes.pm »