- SEE ALSO
- HOME PAGE
Math::NumSeq::Factorials -- factorials i! = 1*2*...*i
use Math::NumSeq::Factorials; my $seq = Math::NumSeq::Factorials->new; my ($i, $value) = $seq->next;
The factorials being product 1*2*3*...*i, 1 to i inclusive.
1, 2, 6, 24, 120, 720, ... starting i=1
See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.
$seq = Math::NumSeq::Factorials->new ()
Create and return a new sequence object.
Move the current sequence position to
$i. The next call to
$iand corresponding value.
$value = $seq->ith($i)
$i==0this is considered an empty product and the return is 1.
$bool = $seq->pred($value)
Return true if
$valueis a factorial, ie. equal to
1*2*...*ifor some i.
$i = $seq->value_to_i($value)
$i = $seq->value_to_i_floor($value)
Return the index i of
$valueis not a factorial then
value_to_i_floor()the i of the next lower value which is or
$value < 1.
$i = $seq->value_to_i_estimate($value)
Return an estimate of the i corresponding to
Value to i Estimate
The current code uses Stirling's approximation
log(n!) ~= n*log(n) - n
by seeking an i for which the target factorial "value" has
i*log(i) - i == log(value)
Newton's method is applied to solve for i,
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)
Just two iterations is quite close
target = log(value) i0 = target i1 = (i0+target)/log(target) = 2*target/log(target) i2 = (i1+target)/log(i1) i ~= int(i2)
Copyright 2010, 2011, 2012, 2013, 2014 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/>.