| 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::Cubes; | ||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use 5.004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use Math::Libm 'cbrt'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use POSIX 'floor','ceil'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use List::Util 'max'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 3 | $VERSION = 75; | |||||||||||||||||||||||||||
| 27 | 3 | ▶ | use Math::NumSeq; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 3 | @ISA = ('Math::NumSeq'); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 31 | #use Smart::Comments; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # use constant name => Math::NumSeq::__('Cubes'); | ||||||||||||||||||||||||||||
| 35 | 3 | ▶ | use constant description => Math::NumSeq::__('The cubes 1, 8, 27, 64, 125, etc, k*k*k.'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | use constant i_start => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 3 | ▶ | use constant values_min => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 3 | ▶ | use constant characteristic_increasing => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 3 | ▶ | use constant characteristic_integer => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | use constant oeis_anum => 'A000578'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use constant 1.02 _UV_I_LIMIT => do { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 43 | # Float integers too when IV=32bits ? | ||||||||||||||||||||||||||||
| 44 | # my $max = 1; | ||||||||||||||||||||||||||||
| 45 | # for (1 .. 256) { | ||||||||||||||||||||||||||||
| 46 | # my $try = $max*2 + 1; | ||||||||||||||||||||||||||||
| 47 | # ### $try | ||||||||||||||||||||||||||||
| 48 | # if ($try == 2*$max || $try == 2*$max+2) { | ||||||||||||||||||||||||||||
| 49 | # last; | ||||||||||||||||||||||||||||
| 50 | # } | ||||||||||||||||||||||||||||
| 51 | # $max = $try; | ||||||||||||||||||||||||||||
| 52 | # } | ||||||||||||||||||||||||||||
| 53 | 3 | my $max = ~0; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 3 | my $bit = 4; | |||||||||||||||||||||||||||
| 56 | 3 | ▶ | for (my $i = $max; $i != 0; $i=int($i/8)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 66 | $bit *= 2; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | ### $bit | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | my $cbrt = 0; | |||||||||||||||||||||||||||
| 62 | 3 | ▶ | for ( ; $bit != 0; $bit=int($bit/2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 75 | my $try = $cbrt + $bit; | |||||||||||||||||||||||||||
| 64 | 75 | ▶ | if ($try * $try <= $max / $try) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 24 | $cbrt = $try; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ### $cbrt | ||||||||||||||||||||||||||||
| 70 | ### cube: $cbrt*$cbrt*$cbrt | ||||||||||||||||||||||||||||
| 71 | ### $max | ||||||||||||||||||||||||||||
| 72 | ### cube hex: sprintf '%X', $cbrt*$cbrt*$cbrt | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | ### assert: $cbrt*$cbrt <= $max/$cbrt | ||||||||||||||||||||||||||||
| 75 | ### assert: ($cbrt+1)*($cbrt+1) > $max/($cbrt+1) | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | $cbrt | |||||||||||||||||||||||||||
| 78 | 3 | }; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 8 | ▶ | sub rewind { | ||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 82 | 8 | $self->{'i'} = $self->i_start; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 23 | ▶ | sub seek_to_i { | ||||||||||||||||||||||||||
|
Subroutine
seek_to_i: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 23 | my ($self, $i) = @_; | |||||||||||||||||||||||||||
| 86 | 23 | ▶ | if ($i >= _UV_I_LIMIT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | $i = Math::NumSeq::_to_bigint($i); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 23 | $self->{'i'} = $i; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 17 | ▶ | sub seek_to_value { | ||||||||||||||||||||||||||
|
Subroutine
seek_to_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 17 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 93 | 17 | $self->seek_to_i($self->value_to_i_ceil($value)); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 50 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 50 | my ($self) = @_; | |||||||||||||||||||||||||||
| 97 | 50 | my $i = $self->{'i'}++; | |||||||||||||||||||||||||||
| 98 | 50 | ▶ | if ($i == _UV_I_LIMIT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | $self->{'i'} = Math::NumSeq::_to_bigint($self->{'i'}); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 50 | return ($i, $i*$i*$i); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 39 | ▶ | sub ith { | ||||||||||||||||||||||||||
|
Subroutine
ith: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 39 | my ($self, $i) = @_; | |||||||||||||||||||||||||||
| 105 | 39 | return $i*$i*$i; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # This used to be a test for cbrt($n) being an integer, but found some amd64 | ||||||||||||||||||||||||||||
| 109 | # glibc where cbrt(27) was not 3 but instead 3.00000000000000044. Dunno if | ||||||||||||||||||||||||||||
| 110 | # cbrt(cube) ought to be an exact integer, so instead try multiplying back | ||||||||||||||||||||||||||||
| 111 | # the integer nearest cbrt(). | ||||||||||||||||||||||||||||
| 112 | # | ||||||||||||||||||||||||||||
| 113 | # Multiplying back should also ensure that a floating point $n bigger than | ||||||||||||||||||||||||||||
| 114 | # 2^53 won't look like a cube due to rounding. | ||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||
| 116 | 258 | ▶ | sub pred { | ||||||||||||||||||||||||||
|
Subroutine
pred: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 258 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 118 | 258 | my $int = int($value); | |||||||||||||||||||||||||||
| 119 | 258 | ▶ | if ($value != $int) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 125 | return 0; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 133 | my $i = _cbrt_floor ($value); | |||||||||||||||||||||||||||
| 123 | 133 | return ($i*$i*$i == $value); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 30 | ▶ | sub value_to_i { | ||||||||||||||||||||||||||
|
Subroutine
value_to_i: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 30 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 128 | 30 | my $int = int($value); | |||||||||||||||||||||||||||
| 129 | 30 | ▶ | if ($value == $int) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 18 | my $i = _cbrt_floor ($int); | |||||||||||||||||||||||||||
| 131 | 18 | ▶ | if ($int == $self->ith($i)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 13 | return $i; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 17 | return undef; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 70 | ▶ | sub value_to_i_floor { | ||||||||||||||||||||||||||
|
Subroutine
value_to_i_floor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 70 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 139 | 70 | return _cbrt_floor($value); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 67 | ▶ | sub value_to_i_ceil { | ||||||||||||||||||||||||||
|
Subroutine
value_to_i_ceil: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 67 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 143 | 67 | return _cbrt_ceil($value); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 3 | *value_to_i_estimate = \&value_to_i_floor; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 149 | # generic, shared | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 636 | ▶ | sub _cbrt_floor { | ||||||||||||||||||||||||||
|
Subroutine
_cbrt_floor: called CC=5 SCAR=20.9
| |||||||||||||||||||||||||||||
| 152 | 636 | my ($x) = @_; | |||||||||||||||||||||||||||
| 153 | 636 | ▶ | if (ref $x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 29 | ▶ | if ($x->isa('Math::BigInt')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 29 | return $x->copy->broot(3); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | if ($x->isa('Math::BigRat') || $x->isa('Math::BigFloat')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$x->isa("Math::BigRat") or $x->isa("Math::BigFloat")
MC/DC0%0/2
$x->isa("Math::BigRat") or $x->isa("Math::BigFloat")
$x->isa("Math::BigRat")
$x->isa("Math::BigFloat")
Truth table0%0/3
A or B
A= $x->isa("Math::BigRat")B= $x->isa("Math::BigFloat")
| |||||||||||||||||||||||||||||
| 158 | 0 | return $x->as_int->broot(3); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 607 | return floor(cbrt($x)); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 67 | ▶ | sub _cbrt_ceil { | ||||||||||||||||||||||||||
|
Subroutine
_cbrt_ceil: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 165 | 67 | my ($x) = @_; | |||||||||||||||||||||||||||
| 166 | 67 | my $c = _cbrt_floor($x); | |||||||||||||||||||||||||||
| 167 | 67 | ▶ | if ($c*$c*$c < $x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 29 | $c += 1; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 67 | return $c; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 3 | 1; | |||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||