1Math::NumSeq::FibonacciU(s3e)r Contributed Perl DocumentaMtaitohn::NumSeq::Fibonacci(3)
2
3
4

NAME

6       Math::NumSeq::Fibonacci -- Fibonacci numbers
7

SYNOPSIS

9        use Math::NumSeq::Fibonacci;
10        my $seq = Math::NumSeq::Fibonacci->new;
11        my ($i, $value) = $seq->next;
12

DESCRIPTION

14       The Fibonacci numbers F(i) = F(i-1) + F(i-2) starting from 0,1,
15
16           0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
17           starting i=0
18

FUNCTIONS

20       See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence
21       classes.
22
23       "$seq = Math::NumSeq::Fibonacci->new ()"
24           Create and return a new sequence object.
25
26   Iterating
27       "($i, $value) = $seq->next()"
28           Return the next index and value in the sequence.
29
30           When $value exceeds the range of a Perl unsigned integer the return
31           is a "Math::BigInt" to preserve precision.
32
33       "$seq->seek_to_i($i)"
34           Move the current sequence position to $i.  The next call to
35           "next()" will return $i and corresponding value.
36
37   Random Access
38       "$value = $seq->ith($i)"
39           Return the $i'th Fibonacci number.
40
41           For negative <$i> the sequence is extended backwards as
42           F[i]=F[i+2]-F[i+1].  The effect is the same Fibonacci numbers but
43           negative at negative even i.
44
45                i     F[i]
46               ---    ----
47                0       0
48               -1       1
49               -2      -1       <----+ negative at even i
50               -3       2            |
51               -4      -3       <----+
52
53           When $value exceeds the range of a Perl unsigned integer the return
54           is a "Math::BigInt" to preserve precision.
55
56       "$bool = $seq->pred($value)"
57           Return true if $value occurs in the sequence, so is a positive
58           Fibonacci number.
59
60       "$i = $seq->value_to_i_estimate($value)"
61           Return an estimate of the i corresponding to $value.  See "Value to
62           i Estimate" below.
63

FORMULAS

65   Ith
66       Fibonacci F[i] can be calculated by a powering procedure with two
67       squares per step.  A pair of values F[k] and F[k-1] are maintained and
68       advanced according to bits of i from high to low
69
70           start k=1, F[k]=1, F[k-1]=0
71           add = -2       # 2*(-1)^k
72
73           loop
74             F[2k+1] = 4*F[k]^2 - F[k-1]^2 + add
75             F[2k-1] =   F[k]^2 + F[k-1]^2
76
77             F[2k] = F[2k+1] - F[2k-1]
78
79             bit = next bit of i, high to low, skip high 1 bit
80             if bit == 1
81                take F[2k+1], F[2k] as new F[k],F[k-1]
82                add = -2 (for next loop)
83             else bit == 0
84                take F[2k], F[2k-1] as new F[k],F[k-1]
85                add = 2 (for next loop)
86
87       For the last (least significant) bit of i an optimization can be made
88       with a single multiple for that last step, instead of two squares.
89
90           bit = least significant bit of i
91           if bit == 1
92              F[2k+1] = (2F[k]+F[k-1])*(2F[k]-F[k-1]) + add
93           else
94              F[2k]   = F[k]*(F[k]+2F[k-1])
95
96       The "add" amount is 2*(-1)^k which means +2 or -2 according to k odd or
97       even, which means whether the previous bit taken from i was 1 or 0.
98       That can be easily noted from each bit, to be used in the following
99       loop iteration or the final step F[2k+1] formula.
100
101       For small i it's usually faster to just successively add
102       F[k+1]=F[k]+F[k-1], but when in bignums the doubling k->2k by two
103       squares is faster than doing k many individual additions for the same
104       thing.
105
106   Value to i Estimate
107       F[i] increases as a power of phi, the golden ratio.  The exact value is
108
109           F[i] = (phi^i - beta^i) / (phi - beta)    # exactly
110
111           phi = (1+sqrt(5))/2 = 1.618
112           beta = -1/phi = -0.618
113
114       Since abs(beta)<1 the beta^i term quickly becomes small.  So taking a
115       log (natural logarithm) to get i,
116
117           log(F[i]) ~= i*log(phi) - log(phi-beta)
118           i ~= (log(F[i]) + log(phi-beta)) / log(phi)
119
120       Or the same using log base 2 which can be estimated from the highest
121       bit position of a bignum,
122
123           log2(F[i]) ~= i*log2(phi) - log2(phi-beta)
124           i ~= (log2(F[i]) + log2(phi-beta)) / log2(phi)
125

SEE ALSO

127       Math::NumSeq, Math::NumSeq::LucasNumbers, Math::NumSeq::Fibbinary,
128       Math::NumSeq::FibonacciWord, Math::NumSeq::Pell,
129       Math::NumSeq::Tribonacci
130
131       Math::Fibonacci, Math::Fibonacci::Phi
132

HOME PAGE

134       <http://user42.tuxfamily.org/math-numseq/index.html>
135

LICENSE

137       Copyright 2010, 2011, 2012, 2013, 2014 Kevin Ryde
138
139       Math-NumSeq is free software; you can redistribute it and/or modify it
140       under the terms of the GNU General Public License as published by the
141       Free Software Foundation; either version 3, or (at your option) any
142       later version.
143
144       Math-NumSeq is distributed in the hope that it will be useful, but
145       WITHOUT ANY WARRANTY; without even the implied warranty of
146       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147       General Public License for more details.
148
149       You should have received a copy of the GNU General Public License along
150       with Math-NumSeq.  If not, see <http://www.gnu.org/licenses/>.
151
152
153
154perl v5.28.0                      2014-06-29        Math::NumSeq::Fibonacci(3)
Impressum