Math::N...rahedral(3)
π Math::NumSeq::Tetrahedral π
okay bestie so we're in the User Contributed Perl Documentation section, and today's main character is Math::NumSeq::Tetrahedral β yes that's the whole name, say it with your chest π
β¨ NAME β¨
Math::NumSeq::Tetrahedral β tetrahedral numbers i*(i+1)*(i+2)/6
like. that's the vibe. lil pyramid-shaped numbers. iconic behavior fr π
πΈ SYNOPSIS πΈ
use Math::NumSeq::Tetrahedral;
my $seq = Math::NumSeq::Tetrahedral->new;
my ($i, $value) = $seq->next;
that's it. that's the whole entrance. you make the object, you call next, the numbers come to you. no cap, it's giving effortless π
π¦ DESCRIPTION π¦
So. The tetrahedral numbers, which are literally i*(i+1)*(i+2)/6.
0, 1, 4, 10, 20, 35, 56, 84, 120, ...
look at them. stacking up like lil cannonballs in a triangle-based pyramid. obsessed. they start at 0 and they just keep going, because that's what sequences DO π
π« FUNCTIONS π«
okay so for the shared behavior that every sequence class has, go peek at "FUNCTIONS" in Math::NumSeq β that's the common lore, iykyk β¨
$seq = Math::NumSeq::Tetrahedral->new ()β makes you a brand new sequence object and hands it over. that's your girl. take care of her π«Ά
π₯ Random Access π₯
$value = $seq->ith($i)β returns$i*($i+1)*($i+2)/6. you ask for index$i, you get the tetrahedral number sitting at that spot. direct. no games π$bool = $seq->pred($value)β returns true if$valuehas the formi*(i+1)*(i+2)/6for some positive integeri. so like. is this number part of the clique or not. vibe check π$i = $seq->value_to_i_floor($value)β gives you back the indexiof$value, or if$valueisn't one of them, the index of the next tetrahedral number below$value. it rounds down, bestie. always down β¬οΈ$i = $seq->value_to_i_estimate($value)β hands you an estimate of theithat corresponds to$value.
lowkey important: in the current code this$igives the tetrahedral above or below$value, so it's out by no more than 1. so it's a guess, but a really really good one. don't be dramatic about it π
π§ FORMULAS π§
β Value to i Estimate β
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
see how it's all cozy between two cubes?? sandwiched. sandwiched like a lil oreo π
for value_to_i_estimate() it's enough to apply a cube root,
i_estimate = floor(cbrt(6*value))
cube root and floor it, done, slay π
π Value to i Floor π
okay so 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 then floor(cbrt(6*57))=6 is correct, but value=58 gives floor(cbrt(6*58))=7 which is 1 too big. rude of it honestly π
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
so we double check and if the cube root overstepped, we knock it back down by one. correction arc complete β¨
π SEE ALSO π
Math::NumSeq, Math::NumSeq::Cubes
go read those too. sibling energy fr π«Ά
π HOME PAGE π
<http://user42.tuxfamily.org/math-numseq/index.html>
π LICENSE π
Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 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/>.
π no warranty bestie. it's free software, it's given to you as-is, and if it breaks that's between you and the GPL. read the license, it's the whole point. periodt.
and that's the whole page!! this one ships as perl v5.42.0, dated 2025-07-25, under the manual name Math::N...rahedral(3) π mwah, go count some pyramids πβ¨
π¦ where this page comes from
- fedora 43: perl-Math-NumSeq 75-11.fc43 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms