Kevin Ryde > Math-NumSeq > Math::NumSeq::Factorials

Download:
Math-NumSeq-71.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  2
View/Report Bugs
Module Version: 71   Source  

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;

DESCRIPTION ^

The factorials being product 1*2*3*...*i, 1 to i inclusive.

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

FUNCTIONS ^

See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.

$seq = Math::NumSeq::Factorials->new ()

Create and return a new sequence object.

Iterating

$seq->seek_to_i($i)

Move the current sequence position to $i. The next call to next() will return $i and corresponding value.

Random Access

$value = $seq->ith($i)

Return 1*2*...*$i. For $i==0 this is considered an empty product and the return is 1.

$bool = $seq->pred($value)

Return true if $value is a factorial, ie. equal to 1*2*...*i for some i.

$i = $seq->value_to_i($value)
$i = $seq->value_to_i_floor($value)

Return the index i of $value. If $value is not a factorial then value_to_i() returns undef, or value_to_i_floor() the i of the next lower value which is or undef if $value < 1.

$i = $seq->value_to_i_estimate($value)

Return an estimate of the i corresponding to $value.

FORMULAS ^

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)

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

syntax highlighting: