File Coverage

blib/lib/Math/NumSeq/CullenNumbers.pm
Criterion Covered Total %
statement 59 68 86.7
branch 8 12 66.6
condition 5 6 83.3
subroutine 15 17 88.2
pod 4 4 100.0
total 91 107 85.0


line stmt bran cond sub pod time code
1             # Copyright 2011, 2012, 2013, 2014 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 .
17              
18             package Math::NumSeq::CullenNumbers;
19 1     1   1030 use 5.004;
  1         3  
20 1     1   6 use strict;
  1         1  
  1         23  
21              
22 1     1   6 use vars '$VERSION', '@ISA';
  1         1  
  1         60  
23             $VERSION = 72;
24              
25 1     1   4 use Math::NumSeq;
  1         2  
  1         48  
26             @ISA = ('Math::NumSeq');
27             *_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     1   4 use constant description => Math::NumSeq::__('Cullen numbers n*2^n+1.');
  1         4  
  1         4  
35 1     1   4 use constant i_start => 0;
  1         1  
  1         37  
36 1     1   5 use constant values_min => 1;
  1         1  
  1         31  
37 1     1   4 use constant characteristic_increasing => 1;
  1         1  
  1         36  
38 1     1   3 use constant characteristic_integer => 1;
  1         1  
  1         32  
39 1     1   3 use constant oeis_anum => 'A002064';
  1         0  
  1         365  
40              
41             # pow*i+1
42             my $uv_i_limit = do {
43             my $max = ~0;
44             my $limit = 1;
45              
46             for (my $i = 1; $i < 1000; $i++) {
47             if ($i <= ($max-1) >> $i) {
48             $limit = $i;
49             } else {
50             last;
51             }
52             }
53              
54             ### max : $max
55             ### cullen: (1<<$limit)*$limit+1
56             ### assert: $limit*(1<<$limit)+1 <= $max
57              
58             $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             sub rewind {
71 3     3 1 156 my ($self) = @_;
72 3         13 my $i = $self->{'i'} = $self->i_start;
73 3         8 $self->{'power'} = 2 ** $i;
74             }
75             sub _UNTESTED__seek_to_i {
76 0     0   0 my ($self, $i) = @_;
77 0         0 $self->{'i'} = $i;
78 0 0       0 if ($i >= $uv_i_limit) {
79 0         0 $i = Math::NumSeq::_to_bigint($i);
80             }
81 0         0 $self->{'power'} = 2 ** $i;
82             }
83             sub _UNTESTED__seek_to_value {
84 0     0   0 my ($self, $value) = @_;
85 0         0 $self->seek_to_i($self->value_to_i_ceil($value));
86             }
87             sub next {
88 20     20 1 125 my ($self) = @_;
89 20         16 my $i = $self->{'i'}++;
90 20 50       24 if ($i == $uv_i_limit) {
91 0         0 $self->{'power'} = Math::NumSeq::_to_bigint($self->{'power'});
92             }
93 20         14 my $value = $self->{'power'}*$i + 1;
94 20         16 $self->{'power'} *= 2;
95 20         18 return ($i, $value);
96             }
97              
98             sub ith {
99 33     33 1 42 my ($self, $i) = @_;
100 33         43 return $i * 2**$i + 1;
101             }
102             sub pred {
103 1803     1803 1 4025 my ($self, $value) = @_;
104             ### CullenNumbers pred(): $value
105              
106             {
107 1803         981 my $int = int($value);
  1803         1167  
108 1803 100       1956 if ($value != $int) { return 0; }
  896         799  
109 907         574 $value = $int;
110             }
111 907 100 66     2278 unless ($value >= 1
112             && ($value % 2) == 1) {
113 448         402 return 0;
114             }
115              
116 459         295 $value -= 1; # now seeking $value == $exp * 2**$exp
117              
118 459 50       561 if (_is_infinite($value)) {
119 0         0 return 0;
120             }
121              
122 459         365 my $exp = 0;
123 459         272 for (;;) {
124 1384 100 100     3054 if ($value <= $exp || $value % 2) {
125 459         564 return ($value == $exp);
126             }
127 925         578 $value = int($value/2);
128 925         543 $exp++;
129             }
130             }
131              
132 1     1   677 use Math::NumSeq::WoodallNumbers;
  1         2  
  1         40  
133             *value_to_i_estimate = \&Math::NumSeq::WoodallNumbers::value_to_i_estimate;
134              
135             1;
136             __END__