Math::N...torials(3)
π 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 callnext()it's gonna hand you back$iand the value that goes with it. slay.
π Random Access π
$value = $seq->ith($i)β returns1*2*...*$i. and if"$i==0"? that counts as an empty product and you get1back. periodt π$bool = $seq->pred($value)β true if$valueis a factorial, like, actually equal to1*2*...*ifor somei. vibe check passed β$i = $seq->value_to_i($value)$i = $seq->value_to_i_floor($value)β these give you the indexiof$value. but heads up bestie π¨ if$valueisn't a factorial thenvalue_to_i()returns"undef", orvalue_to_i_floor()gives you theiof 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 theithat 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
- 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