Math::N...torials(3)

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

πŸ‘‘ Math::N...torials(3) β€” bestie's guide to Math::NumSeq::Factorials

User Contributed Perl Documentation ✨ so this is the page for Math::NumSeq::Factorials, which is the cutest lil sequence module and honestly? iconic. Let's get into it 🫢


✨ NAME ✨

Math::NumSeq::Factorials β€” factorials i! = 1*2*...*i


πŸ’… SYNOPSIS πŸ’…

use Math::NumSeq::Factorials;
my $seq = Math::NumSeq::Factorials->new;
my ($i, $value) = $seq->next;

that's it, that's the whole vibe 🌸


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so the factorials are the product 1*2*3*...*i, and that's from 1 to i inclusive. no skips, no gaps, just multiplying everything along the way, main character energy fr fr πŸ’–

1, 2, 6, 24, 120, 720, ...
starting i=1

like. the way that 720 is right there and it's already giving big numbers?? obsessed 😭


🌈 FUNCTIONS 🌈

go check "FUNCTIONS" in Math::NumSeq for the behaviour that every sequence class has in common, because there's a whole shared personality these modules have, iykyk ✨

  • $seq = Math::NumSeq::Factorials->new () β€” makes you a brand new sequence object and hands it over. that's your girl. πŸ’–

πŸŽ€ Iterating πŸŽ€

  • $seq->seek_to_i($i) β€” moves the current sequence position over to $i. then the next time you call next() it's gonna hand you back $i and the value that goes with it. slay.

πŸ“ Random Access πŸ“

  • $value = $seq->ith($i) β€” returns 1*2*...*$i. and if "$i==0"? that counts as an empty product and you get 1 back. periodt πŸ’…
  • $bool = $seq->pred($value) β€” true if $value is a factorial, like, actually equal to 1*2*...*i for some i. vibe check passed βœ…
  • $i = $seq->value_to_i($value)
  • $i = $seq->value_to_i_floor($value) β€” these give you the index i of $value. but heads up bestie 🚨 if $value isn't a factorial then value_to_i() returns "undef", or value_to_i_floor() gives you the i of the next lower value which is a factorial, or "undef" if "$value < 1". no cap.
  • $i = $seq->value_to_i_estimate($value) β€” returns an estimate of the i that goes with $value. it's giving "close enough" πŸ’«

🧚 FORMULAS 🧚

⭐ Value to i Estimate ⭐

the current code is out here using Stirling's approximation, as one does:

log(n!) ~= n*log(n) - n

and it does that by seeking an i where the target factorial value has

i*log(i) - i == log(value)

then Newton's method gets applied to solve for i, and the whole little setup is:

target=log(value)
f(x) = x*log(x) - x - target      wanting f(x)=0
f'(x) = log(x)

iterate next_x = x - f(x)/f'(x)
               = (x+target)/log(x)

and literally just two iterations is quite close. two!! that's it!! πŸ’€

target = log(value)
i0 = target
i1 = (i0+target)/log(target)
   = 2*target/log(target)
i2 = (i1+target)/log(i1)

i ~= int(i2)

πŸ’œ SEE ALSO πŸ’œ

Math::NumSeq, Math::NumSeq::Primorials

Math::BigInt (bfac()), Math::Combinatorics (factorial(), Math::NumberCruncher (Factorial() Math::BigApprox (Fact()


🌸 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/>.

⚠️ okay so that warranty bit is genuinely important, not just filler β€” there is no warranty, like none, so don't come for anyone if it breaks. read the license for real bestie.

and that's the whole page!! πŸ’– this was perl v5.42.0, dated 2025-07-25, and it's the Math::N...torials(3) manual page. mwah. go multiply some numbers. πŸ‘‘βœ¨

πŸ“¦ where this page comes from