1Math::NumSeq::FactorialUss(e3r)Contributed Perl DocumentMaattiho:n:NumSeq::Factorials(3)
2
3
4
6 Math::NumSeq::Factorials -- factorials i! = 1*2*...*i
7
9 use Math::NumSeq::Factorials;
10 my $seq = Math::NumSeq::Factorials->new;
11 my ($i, $value) = $seq->next;
12
14 The factorials being product 1*2*3*...*i, 1 to i inclusive.
15
16 1, 2, 6, 24, 120, 720, ...
17 starting i=1
18
20 See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence
21 classes.
22
23 "$seq = Math::NumSeq::Factorials->new ()"
24 Create and return a new sequence object.
25
26 Iterating
27 "$seq->seek_to_i($i)"
28 Move the current sequence position to $i. The next call to
29 "next()" will return $i and corresponding value.
30
31 Random Access
32 "$value = $seq->ith($i)"
33 Return "1*2*...*$i". For "$i==0" this is considered an empty
34 product and the return is 1.
35
36 "$bool = $seq->pred($value)"
37 Return true if $value is a factorial, ie. equal to "1*2*...*i" for
38 some i.
39
40 "$i = $seq->value_to_i($value)"
41 "$i = $seq->value_to_i_floor($value)"
42 Return the index i of $value. If $value is not a factorial then
43 "value_to_i()" returns "undef", or "value_to_i_floor()" the i of
44 the next lower value which is or "undef" if "$value < 1".
45
46 "$i = $seq->value_to_i_estimate($value)"
47 Return an estimate of the i corresponding to $value.
48
50 Value to i Estimate
51 The current code uses Stirling's approximation
52
53 log(n!) ~= n*log(n) - n
54
55 by seeking an i for which the target factorial "value" has
56
57 i*log(i) - i == log(value)
58
59 Newton's method is applied to solve for i,
60
61 target=log(value)
62 f(x) = x*log(x) - x - target wanting f(x)=0
63 f'(x) = log(x)
64
65 iterate next_x = x - f(x)/f'(x)
66 = (x+target)/log(x)
67
68 Just two iterations is quite close
69
70 target = log(value)
71 i0 = target
72 i1 = (i0+target)/log(target)
73 = 2*target/log(target)
74 i2 = (i1+target)/log(i1)
75
76 i ~= int(i2)
77
79 Math::NumSeq, Math::NumSeq::Primorials
80
81 Math::BigInt ("bfac()"), Math::Combinatorics ("factorial()",
82 Math::NumberCruncher ("Factorial()" Math::BigApprox ("Fact()"
83
85 <http://user42.tuxfamily.org/math-numseq/index.html>
86
88 Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde
89
90 Math-NumSeq is free software; you can redistribute it and/or modify it
91 under the terms of the GNU General Public License as published by the
92 Free Software Foundation; either version 3, or (at your option) any
93 later version.
94
95 Math-NumSeq is distributed in the hope that it will be useful, but
96 WITHOUT ANY WARRANTY; without even the implied warranty of
97 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
98 General Public License for more details.
99
100 You should have received a copy of the GNU General Public License along
101 with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>.
102
103
104
105perl v5.36.0 2022-07-22 Math::NumSeq::Factorials(3)