Math::N...rahedral(3)

πŸ“¦
version 75 Β· 2 versions
, in fedora 43

πŸ’– 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 $value has the form i*(i+1)*(i+2)/6 for some positive integer i. so like. is this number part of the clique or not. vibe check 😌
  • $i = $seq->value_to_i_floor($value) β€” gives you back the index i of $value, or if $value isn'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 the i that corresponds to $value.
lowkey important: in the current code this $i gives 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