漫談 Fibonacci sequence 唐學明 2013.12.13.. Introduction Leonardo Pisano Bogollo, (c. 1170 –...

Post on 12-Jan-2016

214 views 0 download

Transcript of 漫談 Fibonacci sequence 唐學明 2013.12.13.. Introduction Leonardo Pisano Bogollo, (c. 1170 –...

漫談 Fibonacci sequence

唐學明2013.12.13.

Introduction

• Leonardo Pisano Bogollo, (c. 1170 – c. 1250) also known as Leonardo of Pisa, Leonardo Pisano, Leonardo Bonacci, Leonardo Fibonacci, or, most commonly, simply Fibonacci, was an Italian mathematician, considered by some "the most talented western mathematician of the Middle Ages."

• Filins Bonacci is his son.

Definition of Fibonacci numbers

Fibonacci numbers : F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) for n>1, thus F(2) = 1, F(3) = 2, ...

Fibonacci sequence : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, ......

Fibonacci Block

• A tiling with squares whose sides are successive Fibonacci numbers in length :

31

2

1

5

8

Fibonacci SpiralFibonacci Block

Definition for negative index n

• the sequence can also be extended to negative index n. The result satisfies the equation F-n = (-1)n+1 Fn

• Thus the complete sequence is..., 13, -8, 5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8, 13, ...

Divisibility Property

• Every 3rd number of the sequence is even and more generally, every kth number of the sequence is a multiple of Fk. Thus the Fibonacci sequence is an example of a divisibility sequence. In fact, the Fibonacci sequence satisfies the stronger divisibility

property : GCD(Fm, Fn) = FGCD(m,n)

Divisibility Property E.g.

• F7 | F14 , F7 | F21 , F5 | F10 , …

• GCD(F8 , F12) = GCD(21,144) = 3 = F4 = FGCD(8,12)

• GCD(F14 , F21) = GCD(377, 10946) = 13 = F7 = FGCD(14,21)

Binet's Fibonacci Number Formula

• It was derived by Binet in 1843, although the result was known to Euler, Daniel Bernoulli, and de Moivre more than a century earlier.

• SEE ALSO: Closed-form expression of Fibonacci Number

an can be computed in O(log2 n) time

費氏數列之生成函數

nn

n

nnn

nn

nnn

ccf

r

rr

rrr

rf

fff

2

51

2

51

2

51,

2

51

01

0

21

2

21

21

nn

nf

cc

ccf

ccf

2

51

2

51

5

1

5

1,

5

1解得

12

51

2

51

02

51

2

51

21

1

2

1

11

0

2

0

10

Closed-form expression

Program 1

fib(n) /*fibonacci*/int n;{if(n<3) return(1);else return(fib(n-1)+fib(n-2));}

time complexity : O(?)

T(n) = Ω(2 n/2) (lower bound) T(n) = O(2n) (upper bound) T(n) = Θ(1.618n)) (tight bound)

# of computation = # of internal nodes = 1.618n

time complexity :

T(n) < 2 T(n1) < 2 2 T(n2) < 2 2 2 T(n3)...< 2 2 2 2 … 2 T(0)

T(n) = Ω(2n/2) (lower bound) T(n) = O(2n) (upper bound) T(n) = Θ(1.618n) (tight bound)

T(n) > 2 T(n2) > 2 2 T(n4) > 2 2 2 T(n6)...< 2 2 2 2 … 2 T(0)

n timesn/2 times

Program 2fib(n) /*fibonacci*/int n;{int i, p, p1, p2;if(n<3) return(1);p1 = 1; p2 = 1;for(i=3; i<=n; i++) { p = p1 + p2; p2 = p1; p1 = p; }return(p);} time complexity : O(n)

iteration version of Program 1

Basic Idea

Fibonacci sequence : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ….

F1 = F2 = 1

F2n+1 = Fn2 + Fn+1

2, n > 0

F2n = 2 Fn-1Fn + Fn

2, n > 1 e.g.

F11 = F52 + F6

2 = 25 + 64 = 89

F12 = 2 F5 F6 + F62 = 80 + 64 = 144

Program 3

fib(n) /*fibonacci*/int n;{if(n<3) return(1);if(n%2==1) /* n is odd */ return( fib((n-1)/2)*fib((n-1)/2) + fib((n+1)/2)*

fib((n+1)/2) );else /* n is even */ return( 2*fib(n/2-1)*fib(n/2) + fib(n/2)* fib(n/2) );}

time complexity : O(n)

Divide and Conquer !

How to get a iteration version of Program 3 (1/2)

F4 = F2 F2 + 2 F2 F1 = 3

F3 = F2 F2 + F1 F1 = 2

F8 = F4 F4 + 2 F4 F3 = 21

F7 = F4 F4 + F3 F3 = 13

F2^k = F2^(k-1) F2^(k-1) + 2 F2^(k-1) F2^(k-1)-1

F2^k-1 = F2^(k-1) F2^(k-1) + F2^(k-1)-1 F2^(k-1)-1

e.g.

For n = 2k -1 or 2k , compute Fn takes O(log2 n) time.However, how about other n’s ?

Derived from basic idea

F16 = F8 F8 + 2 F8 F7 = 987

F15 = F8 F8 + F7 F7 = 610

How to get a iteration version of Program 3 (2/2)

F1+2-1 = F1 F2 + F0 F1 = 1

F1+2 = F1 F2 + F1 F1 + F0 F2

= 2

Fi+j-1 = Fi Fj + Fi-1 Fj-1

Fi+j = Fi Fj + Fi Fj-1 + Fi-1 Fj

e.g.

We apply new formulas !

F3+16-1 = F3 F16 + F2 F15 = 2584

F3+16 = F3 F16 + F3 F15 + F2 F16

= 4181

Program 4fib(n) /*fibonacci*/int n;{unsigned long p=1;unsigned long q=0;unsigned long a=0;unsigned long b=1;unsigned long t;for(;n!=0;n>>=1){ if(n&0x01!=0){ /* n is odd */ t=a*(p+q)+b*p; b=a*p+b*q; a=t; } t=p*p+2*p*q; q=p*p+q*q; p=t; }return(a);} time complexity : O(log2 n)

iteration version of Program 3, the best one

Another program with O(log2 n)

Fn = 1 Fn-1 + 1 Fn-2

Fn-1 = 1 Fn-1 + 0 Fn-2

01

11

Fn-1

Fn

Fn-2

Fn-1=

01

11

Fn-3

Fn-2=

2

01

11

F1

F2= … =

n-2

= … =

01

11

Fn-i-1

Fn-ii

01

11

1

1=

n-2

Another program with O(log2 n) (cont.)

F8

F9=

01

11

1

1=

7

01

11

1

13 2

01

11

=

01

11

1

12

01

11

01

112

=

01

11

1

12

11

12

01

11

=

01

11

1

12

12

23=

01

11

58

813

1

1

813

1321

1

1= =

21

34

Thank You

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi 時間複雜度分析

time complexity : O(2n)