You are on page 1of 51

Contents

1 Computing Fibonacci
numbers

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 1 / 11


Computing Fibonacci numbers

Section outline

The Q-connection to Fn
Illustration of matrix power
1 Computing Fibonacci method for F34
numbers Reversed odd-even
Direct computation from reconstruction with doubling
recursive Fn definition Illustration of reversed
Improved recursive odd-even reconstruction
computing of Fn with doubling

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 2 / 11


Computing Fibonacci numbers Direct computation from recursive Fn definition

Direct computation from recursive Fn definition



 0 n=0
Fn = 1 n=1
Fn−1 + Fn−2 n > 1

Time complexity for computing Fn by this recursive formulation

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 3 / 11


Computing Fibonacci numbers Direct computation from recursive Fn definition

Direct computation from recursive Fn definition



 0 n=0 F2 = 1 F3 = 2 F4 = 3
Fn = 1 n=1 F5 = 5 F6 = 8 F7 = 13
Fn−1 + Fn−2 n > 1 F8 = 21 F9 = 34 F10 = 55

Time complexity for computing Fn by this recursive formulation

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 3 / 11


Computing Fibonacci numbers Direct computation from recursive Fn definition

Direct computation from recursive Fn definition



 0 n=0 F2 = 1 F3 = 2 F4 = 3
Fn = 1 n=1 F5 = 5 F6 = 8 F7 = 13
Fn−1 + Fn−2 n > 1 F8 = 21 F9 = 34 F10 = 55

int fibD(int n) {
if (n == 0) return 0 else
if (n == 1) return 1 else
return fibD(n-1) + fibD(n-2);
}
Time complexity for computing Fn by this recursive formulation

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 3 / 11


Computing Fibonacci numbers Direct computation from recursive Fn definition

Direct computation from recursive Fn definition



 0 n=0 F2 = 1 F3 = 2 F4 = 3
Fn = 1 n=1 F5 = 5 F6 = 8 F7 = 13
Fn−1 + Fn−2 n > 1 F8 = 21 F9 = 34 F10 = 55

int fibD(int n) {
if (n == 0) return 0 else
if (n == 1) return 1 else
return fibD(n-1) + fibD(n-2);
}
Time complexity for computing Fn by this recursive formulation
T (n) = T (n − 1) + T (n − 2) + C; T (0) =?; T (1) =?

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 3 / 11


Computing Fibonacci numbers Direct computation from recursive Fn definition

Direct computation from recursive Fn definition



 0 n=0 F2 = 1 F3 = 2 F4 = 3
Fn = 1 n=1 F5 = 5 F6 = 8 F7 = 13
Fn−1 + Fn−2 n > 1 F8 = 21 F9 = 34 F10 = 55

int fibD(int n) {
if (n == 0) return 0 else
if (n == 1) return 1 else
return fibD(n-1) + fibD(n-2);
}
Time complexity for computing Fn by this recursive formulation
T (n) = T (n − 1) + T (n − 2) + C; T (0) =?; T (1) =?
T (n) + C = T (n − 1) + C + T (n − 2) + C

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 3 / 11


Computing Fibonacci numbers Direct computation from recursive Fn definition

Direct computation from recursive Fn definition



 0 n=0 F2 = 1 F3 = 2 F4 = 3
Fn = 1 n=1 F5 = 5 F6 = 8 F7 = 13
Fn−1 + Fn−2 n > 1 F8 = 21 F9 = 34 F10 = 55

int fibD(int n) {
if (n == 0) return 0 else
if (n == 1) return 1 else
return fibD(n-1) + fibD(n-2);
}
Time complexity for computing Fn by this recursive formulation
T (n) = T (n − 1) + T (n − 2) + C; T (0) =?; T (1) =?
T (n) + C = T (n − 1) + C + T (n − 2) + C
Let T (n) + C = F (n + k ) ≡ T (n) = F (n + k ) − C, k =?
Let C = 1 and k = 3, so that T (n) = F (n + 3) − 1, then
T (0) = F (0 + 3) − 1 = 2 − 1 = 1 and TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

T (1) = F (1 + 3) − 1 = 3 − 1 = 2, matching well with the code


19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 3 / 11


Computing Fibonacci numbers Improved recursive computing of Fn

Improved recursive computing of Fn



 0 n=0 F2 = 1 F3 = 2 F4 = 3
Fn = 1 n=1 F5 = 5 F6 = 8 F7 = 13
Fn−1 + Fn−2 n > 1 F8 = 21 F9 = 34 F10 = 55

int fibTRLin(int n, int val, int prev ) {


// call fibTRLin() initially with val=1 and prev=0
if(n == 0)
return prev;
if(n == 1)
return val;
else
return fibTRLin(n-1, val+prev, val);
}
Only Fn−1 and Fn−2 are used to compute Fn
– dynamic programming, using solution to smaller subproblems TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

Method works in linear time – routine is tail recursive 19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 4 / 11


Computing Fibonacci numbers The Q-connection to Fn

The Q-connection to Fn

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 5 / 11


Computing Fibonacci numbers The Q-connection to Fn

The Q-connection to Fn

1 1  
1 1
≡ – the Q-matrix
1 0 1 0

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 5 / 11


Computing Fibonacci numbers The Q-connection to Fn

The Q-connection to Fn

1 1    
1 1 F2 F1
≡ ≡ ≡Q
1 0 1 0 F1 F0

 
? Fn+1 Fn
Qn =
Fn Fn−1

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 5 / 11


Computing Fibonacci numbers The Q-connection to Fn

The Q-connection to Fn

1 1    
1 1 F2 F1
≡ ≡ ≡Q
1 0 1 0 F1 F0

 
? Fn+1 Fn Check inductively, satisfied for
Qn =
Fn Fn−1 n = 1, as F0 = 0, F1 = F2 = 1

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 5 / 11


Computing Fibonacci numbers The Q-connection to Fn

The Q-connection to Fn

1 1    
1 1 F2 F1
≡ ≡ ≡Q
1 0 1 0 F1 F0

 
? Fn+1 Fn Check inductively, satisfied for
Qn =
Fn Fn−1 n = 1, as F0 = 0, F1 = F2 = 1
    
n+1 n Fn+1 Fn 1 1 Fn+1 + Fn Fn+1
Q =Q Q= =
Fn Fn−1 1 0 Fn + Fn−1 Fn
 
n+1 Fn+2 Fn+1 TE
OF
TECHNO
LO

GY
Thus, Q

ITU
=

IAN INST

KH
ARAGPUR
IND
 

Fn+1 Fn
19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 5 / 11


Computing Fibonacci numbers The Q-connection to Fn

The Q-connection to Fn

1 1    
1 1 F2 F1
≡ ≡ ≡Q
1 0 1 0 F1 F0

 
? Fn+1 Fn Check inductively, satisfied for
Qn =
Fn Fn−1 n = 1, as F0 = 0, F1 = F2 = 1
    
n+1 n Fn+1 Fn 1 1 Fn+1 + Fn Fn+1
Q =Q Q= =
Fn Fn−1 1 0 Fn + Fn−1 Fn
 
n+1 Fn+2 Fn+1 Does this give us a computational TE
OF
TECHNO
LO

GY
Thus, Q

ITU
=

IAN INST

KH
ARAGPUR
IND
 

Fn+1 Fn procedure to compute Fn ?


19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 5 / 11


Computing Fibonacci numbers The Q-connection to Fn

Matrix power method for Fn computation


 
Fn+1 Fn
Qn =
Fn Fn−1

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 6 / 11


Computing Fibonacci numbers The Q-connection to Fn

Matrix power method for Fn computation


 
Fn+1 Fn
Qn =
Fn Fn−1
Fn can be computed from Q n−1

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 6 / 11


Computing Fibonacci numbers The Q-connection to Fn

Matrix power method for Fn computation


 
Fn+1 Fn
Qn =
Fn Fn−1
Fn can be computed from Q n−1
For computing Q k , let k(2) ≡ bl . . . bp . . . b0 , bi ∈ {0, 1} (k in binary)

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 6 / 11


Computing Fibonacci numbers The Q-connection to Fn

Matrix power method for Fn computation


 
Fn+1 Fn
Qn =
Fn Fn−1
Fn can be computed from Q n−1
For computing Q k , let k(2) ≡ bl . . . bp . . . b0 , bi ∈ {0, 1} (k in binary)
bi 2i ,
P
Thus, Q k = Q i l ≤i ≤0

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 6 / 11


Computing Fibonacci numbers The Q-connection to Fn

Matrix power method for Fn computation


 
Fn+1 Fn
Qn =
Fn Fn−1
Fn can be computed from Q n−1
For computing Q k , let k(2) ≡ bl . . . bp . . . b0 , bi ∈ {0, 1} (k in binary)
bi 2i ,
P
Thus, Q k = Q i l ≤i ≤0
2i i
P Y
Qk = Q i = Q2
bi 6=0
bi 6=0

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 6 / 11


Computing Fibonacci numbers The Q-connection to Fn

Matrix power method for Fn computation


 
Fn+1 Fn
Qn =
Fn Fn−1
Fn can be computed from Q n−1
For computing Q k , let k(2) ≡ bl . . . bp . . . b0 , bi ∈ {0, 1} (k in binary)
bi 2i ,
P
Thus, Q k = Q i l ≤i ≤0
2i i
P Y
Qk = Q i = Q2
bi 6=0
bi 6=0
i i−1 ·2
 i−1 2
Now, Q 2 = Q 2 = Q2

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 6 / 11


Computing Fibonacci numbers The Q-connection to Fn

Matrix power method for Fn computation


 
Fn+1 Fn
Qn =
Fn Fn−1
Fn can be computed from Q n−1
For computing Q k , let k(2) ≡ bl . . . bp . . . b0 , bi ∈ {0, 1} (k in binary)
bi 2i ,
P
Thus, Q k = Q i l ≤i ≤0
2i i
P Y
Qk = Q i = Q2
bi 6=0
bi 6=0
i i−1 ·2
 i−1 2
Now, Q 2 = Q 2 = Q2
Thus, starting with P = I and T = Q, intermediate terms are
computed by repeatedly squaring T and multiplying with P if
bi 6= 0
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 6 / 11


Computing Fibonacci numbers The Q-connection to Fn

Matrix power method for Fn computation


 
Fn+1 Fn
Qn =
Fn Fn−1
Fn can be computed from Q n−1
For computing Q k , let k(2) ≡ bl . . . bp . . . b0 , bi ∈ {0, 1} (k in binary)
bi 2i ,
P
Thus, Q k = Q i l ≤i ≤0
2i i
P Y
Qk = Q i = Q2
bi 6=0
bi 6=0
i i−1 ·2
 i−1 2
Now, Q 2 = Q 2 = Q2
Thus, starting with P = I and T = Q, intermediate terms are
computed by repeatedly squaring T and multiplying with P if
bi 6= 0
Done in Θ(lg n) steps – such notations to be studied later in more TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ARAGPUR
IND
detail
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 6 / 11


Computing Fibonacci numbers Illustration of matrix power method for F34

Illustration of matrix power method for F34


F34 F33
≡Q 33 , need to compute Q 33 to get F34
F33 F32
5 +20 5 0
1 33(10) : 100001(2) (binary) (≡ 25 + 20 ), Q 33 = Q 2 = Q2 Q2
0
2 Start with Qw = Qt = Q 2
 0 2 0 0 1
3 Compute Qt ← Qt 2 = Q 2 = Q2 · Q2 = Q2
 1 2 1 1 2
4 Compute Qt ← Qt 2 = Q 2 = Q2 · Q2 = Q2
 2 2 2 2 3
5 Compute Qt ← Qt 2 = Q 2 = Q2 · Q2 = Q2
 3 2 3 3 4
6 Compute Qt ← Qt 2 = Q 2 = Q2 · Q2 = Q2
 4 2 4 4 5
7 Compute Qt ← Qt 2 = Q 2 = Q2 · Q2 = Q2
TECHNO
OF LO
TE

GY
ITU
5 0

IAN INST

KH
Compute Qw ← Qt · Qw = Q 2 Q 2 =Q 33 X

ARAGPUR
8

IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 7 / 11


Computing Fibonacci numbers Illustration of matrix power method for F34

Some observations

Three methods for computing Fibonacci numbers were studied

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 8 / 11


Computing Fibonacci numbers Illustration of matrix power method for F34

Some observations

Three methods for computing Fibonacci numbers were studied


Time complexities for the methods vary from exponential to linear
to logarithmic

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 8 / 11


Computing Fibonacci numbers Illustration of matrix power method for F34

Some observations

Three methods for computing Fibonacci numbers were studied


Time complexities for the methods vary from exponential to linear
to logarithmic
Optimisation form exponential to linear was easy

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 8 / 11


Computing Fibonacci numbers Illustration of matrix power method for F34

Some observations

Three methods for computing Fibonacci numbers were studied


Time complexities for the methods vary from exponential to linear
to logarithmic
Optimisation form exponential to linear was easy
Optimisation form linear to logarithmic was less obvious

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 8 / 11


Computing Fibonacci numbers Illustration of matrix power method for F34

Some observations

Three methods for computing Fibonacci numbers were studied


Time complexities for the methods vary from exponential to linear
to logarithmic
Optimisation form exponential to linear was easy
Optimisation form linear to logarithmic was less obvious
Other identities can also be used, eg

F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 8 / 11


Computing Fibonacci numbers Illustration of matrix power method for F34

Some observations

Three methods for computing Fibonacci numbers were studied


Time complexities for the methods vary from exponential to linear
to logarithmic
Optimisation form exponential to linear was easy
Optimisation form linear to logarithmic was less obvious
Other identities can also be used, eg

F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

How does this compare to the matrix power method?

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 8 / 11


Computing Fibonacci numbers Illustration of matrix power method for F34

Some observations

Three methods for computing Fibonacci numbers were studied


Time complexities for the methods vary from exponential to linear
to logarithmic
Optimisation form exponential to linear was easy
Optimisation form linear to logarithmic was less obvious
Other identities can also be used, eg

F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

How does this compare to the matrix power method?


Issue of overflow not considered ...
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 8 / 11


Computing Fibonacci numbers Illustration of matrix power method for F34

Some observations

Three methods for computing Fibonacci numbers were studied


Time complexities for the methods vary from exponential to linear
to logarithmic
Optimisation form exponential to linear was easy
Optimisation form linear to logarithmic was less obvious
Other identities can also be used, eg

F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

How does this compare to the matrix power method?


Issue of overflow not considered ...
No fixed method to improve the performance of algorithms, TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ingenuity needed

ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 8 / 11


Computing Fibonacci numbers Reversed odd-even reconstruction with doubling

Reversed odd-even reconstruction with doubling


   2n  n !2
F2n+1 F2n 1 1 1 1
= =
F2n F2n−1 1 0 1 0
 n 2 
! 2
1 1 F Fn
= n+1 =
1 0 Fn Fn−1
   
Fn+1 Fn F Fn
· n+1 =
Fn Fn−1 Fn Fn−1
2 + Fn2
 
Fn+1 Fn+1 Fn + Fn Fn−1
Fn Fn+1 + Fn−1 Fn Fn2 + Fn−1
2

Equating, corresponding term,


2
F2n+1 = Fn+1 + Fn2
 
F2n = Fn [Fn+1 + Fn−1 ] = Fn F(n+1 + (Fn+1 − Fn ) =
TECHNO
OF LO
TE

Fn [2Fn+1 − Fn ]

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 9 / 11


Computing Fibonacci numbers Reversed odd-even reconstruction with doubling

Reversed odd-even reconstruction with doubling


F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

Knowing Fk and Fk+1 , F2k and F2k +1 can be computed

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 10 / 11


Computing Fibonacci numbers Reversed odd-even reconstruction with doubling

Reversed odd-even reconstruction with doubling


F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

Knowing Fk and Fk+1 , F2k and F2k +1 can be computed


Method starts with F1 and F2 (= F1 + F0 ) for k = 1

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 10 / 11


Computing Fibonacci numbers Reversed odd-even reconstruction with doubling

Reversed odd-even reconstruction with doubling


F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

Knowing Fk and Fk+1 , F2k and F2k +1 can be computed


Method starts with F1 and F2 (= F1 + F0 ) for k = 1
We want to compute Fn , let n(2) ≡ b` , b`−1 , . . . , b0 , b` = 1 (n in binary)

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 10 / 11


Computing Fibonacci numbers Reversed odd-even reconstruction with doubling

Reversed odd-even reconstruction with doubling


F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

Knowing Fk and Fk+1 , F2k and F2k +1 can be computed


Method starts with F1 and F2 (= F1 + F0 ) for k = 1
We want to compute Fn , let n(2) ≡ b` , b`−1 , . . . , b0 , b` = 1 (n in binary)
Build n stepwise: n1 = b` = 1, nj = 2nj−1 + b`−j+1 and n = n`−1

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 10 / 11


Computing Fibonacci numbers Reversed odd-even reconstruction with doubling

Reversed odd-even reconstruction with doubling


F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

Knowing Fk and Fk+1 , F2k and F2k +1 can be computed


Method starts with F1 and F2 (= F1 + F0 ) for k = 1
We want to compute Fn , let n(2) ≡ b` , b`−1 , . . . , b0 , b` = 1 (n in binary)
Build n stepwise: n1 = b` = 1, nj = 2nj−1 + b`−j+1 and n = n`−1
Using Fnj−1 and Fnj−1 +1 , compute F2nj−1 and F2nj−1 +1

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 10 / 11


Computing Fibonacci numbers Reversed odd-even reconstruction with doubling

Reversed odd-even reconstruction with doubling


F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

Knowing Fk and Fk+1 , F2k and F2k +1 can be computed


Method starts with F1 and F2 (= F1 + F0 ) for k = 1
We want to compute Fn , let n(2) ≡ b` , b`−1 , . . . , b0 , b` = 1 (n in binary)
Build n stepwise: n1 = b` = 1, nj = 2nj−1 + b`−j+1 and n = n`−1
Using Fnj−1 and Fnj−1 +1 , compute F2nj−1 and F2nj−1 +1
If b`−j+1 = 1, nj = 2nj−1 + 1, so F2nj−1 +1 and F2nj−1 +2 will be needed

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 10 / 11


Computing Fibonacci numbers Reversed odd-even reconstruction with doubling

Reversed odd-even reconstruction with doubling


F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

Knowing Fk and Fk+1 , F2k and F2k +1 can be computed


Method starts with F1 and F2 (= F1 + F0 ) for k = 1
We want to compute Fn , let n(2) ≡ b` , b`−1 , . . . , b0 , b` = 1 (n in binary)
Build n stepwise: n1 = b` = 1, nj = 2nj−1 + b`−j+1 and n = n`−1
Using Fnj−1 and Fnj−1 +1 , compute F2nj−1 and F2nj−1 +1
If b`−j+1 = 1, nj = 2nj−1 + 1, so F2nj−1 +1 and F2nj−1 +2 will be needed
Compute F2nj−1 +2 = F2nj−1 +1 + F2nj−1 and continue

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 10 / 11


Computing Fibonacci numbers Reversed odd-even reconstruction with doubling

Reversed odd-even reconstruction with doubling


F2k = Fk [2Fk+1 − Fk ]
2
F2k+1 = Fk+1 + Fk2

Knowing Fk and Fk+1 , F2k and F2k +1 can be computed


Method starts with F1 and F2 (= F1 + F0 ) for k = 1
We want to compute Fn , let n(2) ≡ b` , b`−1 , . . . , b0 , b` = 1 (n in binary)
Build n stepwise: n1 = b` = 1, nj = 2nj−1 + b`−j+1 and n = n`−1
Using Fnj−1 and Fnj−1 +1 , compute F2nj−1 and F2nj−1 +1
If b`−j+1 = 1, nj = 2nj−1 + 1, so F2nj−1 +1 and F2nj−1 +2 will be needed
Compute F2nj−1 +2 = F2nj−1 +1 + F2nj−1 and continue
Time complexity: lg n steps, up to two multiplications and three additions
in each step; O(lg n), but more efficient than the matrix power method TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 10 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012
1 1 0 1 n1 = 1; F2 = F1 = 1

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012
1 1 0 1 n1 = 1; F2 = F1 = 1
1 1 0 1 n2 = 2n1 + 1 = 3

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012
1 1 0 1 n1 = 1; F2 = F1 = 1
1 1 0 1 n2 = 2n1 + 1 = 3
F2 = F1 × [2 × F2 − F1 ] = 1, F3 = F22 + F12 = 2

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012
1 1 0 1 n1 = 1; F2 = F1 = 1
1 1 0 1 n2 = 2n1 + 1 = 3
F2 = F1 × [2 × F2 − F1 ] = 1, F3 = F22 + F12 = 2
but, F3 and F4 are needed, so, F4 = F3 + F2 = 3

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012
1 1 0 1 n1 = 1; F2 = F1 = 1
1 1 0 1 n2 = 2n1 + 1 = 3
F2 = F1 × [2 × F2 − F1 ] = 1, F3 = F22 + F12 = 2
but, F3 and F4 are needed, so, F4 = F3 + F2 = 3
1 1 0 1 n3 = 2n2 = 6

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012
1 1 0 1 n1 = 1; F2 = F1 = 1
1 1 0 1 n2 = 2n1 + 1 = 3
F2 = F1 × [2 × F2 − F1 ] = 1, F3 = F22 + F12 = 2
but, F3 and F4 are needed, so, F4 = F3 + F2 = 3
1 1 0 1 n3 = 2n2 = 6
F6 = F3 × [2 × F4 − F3 ] = 8, F7 = F42 + F32 = 13

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012
1 1 0 1 n1 = 1; F2 = F1 = 1
1 1 0 1 n2 = 2n1 + 1 = 3
F2 = F1 × [2 × F2 − F1 ] = 1, F3 = F22 + F12 = 2
but, F3 and F4 are needed, so, F4 = F3 + F2 = 3
1 1 0 1 n3 = 2n2 = 6
F6 = F3 × [2 × F4 − F3 ] = 8, F7 = F42 + F32 = 13
1 1 0 1 n = n4 = 2n3 + 1 = 13

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012
1 1 0 1 n1 = 1; F2 = F1 = 1
1 1 0 1 n2 = 2n1 + 1 = 3
F2 = F1 × [2 × F2 − F1 ] = 1, F3 = F22 + F12 = 2
but, F3 and F4 are needed, so, F4 = F3 + F2 = 3
1 1 0 1 n3 = 2n2 = 6
F6 = F3 × [2 × F4 − F3 ] = 8, F7 = F42 + F32 = 13
1 1 0 1 n = n4 = 2n3 + 1 = 13
F12 = F6 × [2 × F7 − F6 ] = 144, F13 = F72 + F62 = 233X
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11


Illustration of reversed odd-even reconstruction with
Computing Fibonacci numbers doubling

Illustration of reversed odd-even reconstruction


with doubling
Example (Computing F13 by this method)
1 1 0 1 n = 1310 ≡ 11012
1 1 0 1 n1 = 1; F2 = F1 = 1
1 1 0 1 n2 = 2n1 + 1 = 3
F2 = F1 × [2 × F2 − F1 ] = 1, F3 = F22 + F12 = 2
but, F3 and F4 are needed, so, F4 = F3 + F2 = 3
1 1 0 1 n3 = 2n2 = 6
F6 = F3 × [2 × F4 − F3 ] = 8, F7 = F42 + F32 = 13
1 1 0 1 n = n4 = 2n3 + 1 = 13
F12 = F6 × [2 × F7 − F6 ] = 144, F13 = F72 + F62 = 233X
F13 and F14 would be needed for further steps, but not needed
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

now
yog, kms kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms August 6, 2017 11 / 11

You might also like