Math::NumSeq::Tetrahedral -- tetrahedral numbers i*(i+1)*(i+2)/6
use Math::NumSeq::Tetrahedral; my $seq = Math::NumSeq::Tetrahedral->new; my ($i, $value) = $seq->next;
The tetrahedral numbers, i*(i+1)*(i+2)/6.
0, 1, 4, 10, 20, 35, 56, 84, 120, ...
See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.
$seq = Math::NumSeq::Tetrahedral->new (key=>value,...)
Create and return a new sequence object.
$value = $seq->ith($i)
Return $i*($i+1)*($i+2)/6.
$i*($i+1)*($i+2)/6
$bool = $seq->pred($value)
Return true if $value has the form i*(i+1)*(i+2)/6 for some positive integer i.
$value
$i = $seq->value_to_i_floor($value)
Return the index i of $value or of the next tetrahedral number below $value.
$i = $seq->value_to_i_estimate($value)
Return an estimate of the i corresponding to $value.
In the current code this $i gives the tetrahedral above or below $value, so is out by no more than 1.
$i
i*(i+1)*(i+2) always fall in between cubes, so
T(i) = i*(i+1)*(i+2)/6 = (i^3 + 3*i^2 + 2*i)/6 i^3 < 6*T(i) < (i+1)^3
For value_to_i_estimate() it's enough to apply a cube root,
value_to_i_estimate()
i_estimate = floor(cbrt(6*value))
For value_to_i_floor() the cube root can be 1 too big when the given value is in between successive T() tetrahedrals. For example if value=57 floor(cbrt(6*57))=6 is correct, but value=58 floor(cbrt(6*58))=7 is 1 too big.
value_to_i_floor()
i = floor(cbrt(6*value)) if i*(i+1)*(i+2) <= 6*value then i_floor = i else i_floor = i-1 # cbrt was 1 too big
Math::NumSeq, Math::NumSeq::Cubes
http://user42.tuxfamily.org/math-numseq/index.html
Copyright 2010, 2011, 2012 Kevin Ryde
Math-NumSeq is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
Math-NumSeq is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>.
To install Math::NumSeq, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::NumSeq
CPAN shell
perl -MCPAN -e shell install Math::NumSeq
For more information on module installation, please visit the detailed CPAN module installation guide.