Presentation is loading. Please wait.

Presentation is loading. Please wait.

漫談 Fibonacci sequence 唐學明 2013.12.13.. Introduction Leonardo Pisano Bogollo, (c. 1170 – c. 1250) also known as Leonardo of Pisa, Leonardo Pisano, Leonardo.

Similar presentations


Presentation on theme: "漫談 Fibonacci sequence 唐學明 2013.12.13.. Introduction Leonardo Pisano Bogollo, (c. 1170 – c. 1250) also known as Leonardo of Pisa, Leonardo Pisano, Leonardo."— Presentation transcript:

1 漫談 Fibonacci sequence 唐學明 2013.12.13.

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

3 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,......

4 Fibonacci Block A tiling with squares whose sides are successive Fibonacci numbers in length : Fibonacci Spiral Fibonacci Block

5 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 F n Thus the complete sequence is..., 13, -8, 5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8, 13,...

6 Divisibility Property Every 3rd number of the sequence is even and more generally, every kth number of the sequence is a multiple of F k. Thus the Fibonacci sequence is an example of a divisibility sequence. In fact, the Fibonacci sequence satisfies the stronger divisibility property : GCD(F m, F n ) = F GCD(m,n)

7 Divisibility Property E.g. F 7 | F 14, F 7 | F 21, F 5 | F 10, … GCD(F 8, F 12 ) = GCD(21,144) = 3 = F 4 = F GCD(8,12) GCD(F 14, F 21 ) = GCD(377, 10946) = 13 = F 7 = F GCD(14,21)

8 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 a n can be computed in O(log 2 n) time

9 費氏數列之生成函數

10 Closed-form expression

11 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(2 n ) (upper bound) T(n) = Θ(1.618 n )) (tight bound)

12 # of computation = # of internal nodes = 1.618 n

13 time complexity : T(n) < 2  T(n  1) < 2  2  T(n  2) < 2  2  2  T(n  3). < 2  2  2  2  …  2  T(0) T(n) = Ω(2 n/2 ) (lower bound) T(n) = O(2 n ) (upper bound) T(n) = Θ(1.618 n ) (tight bound) T(n) > 2  T(n  2) > 2  2  T(n  4) > 2  2  2  T(n  6). < 2  2  2  2  …  2  T(0) n times n/2 times

14 Program 2 fib(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

15 Basic Idea Fibonacci sequence : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …. F 1 = F 2 = 1 F 2n+1 = F n 2 + F n+1 2, n > 0 F 2n = 2 F n-1 F n + F n 2, n > 1 e.g. F 11 = F 5 2 + F 6 2 = 25 + 64 = 89 F 12 = 2 F 5 F 6 + F 6 2 = 80 + 64 = 144

16 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 !

17 How to get a iteration version of Program 3 (1/2) F 4 = F 2 F 2 + 2 F 2 F 1 = 3 F 3 = F 2 F 2 + F 1 F 1 = 2 F 8 = F 4 F 4 + 2 F 4 F 3 = 21 F 7 = F 4 F 4 + F 3 F 3 = 13 F 2^k = F 2^(k-1) F 2^(k-1) + 2 F 2^(k-1) F 2^(k-1)-1 F 2^k-1 = F 2^(k-1) F 2^(k-1) + F 2^(k-1)-1 F 2^(k-1)-1 e.g. For n = 2 k -1 or 2 k, compute F n takes O(log 2 n) time. However, how about other n’s ? Derived from basic idea F 16 = F 8 F 8 + 2 F 8 F 7 = 987 F 15 = F 8 F 8 + F 7 F 7 = 610

18 How to get a iteration version of Program 3 (2/2) F 1+2-1 = F 1 F 2 + F 0 F 1 = 1 F 1+2 = F 1 F 2 + F 1 F 1 + F 0 F 2 = 2 F i+j-1 = F i F j + F i-1 F j-1 F i+j = F i F j + F i F j-1 + F i-1 F j e.g. We apply new formulas ! F 3+16-1 = F 3 F 16 + F 2 F 15 = 2584 F 3+16 = F 3 F 16 + F 3 F 15 + F 2 F 16 = 4181

19 Program 4 fib(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(log 2 n) iteration version of Program 3, the best one

20 Another program with O(log 2 n) F n = 1 F n-1 + 1 F n-2 F n-1 = 1 F n-1 + 0 F n-2       01 11       F n-1 FnFn       F n-2 F n-1 =       01 11       F n-3 F n-2 = 2       01 11       F1F1 F2F2 = … = n-2 = … =       01 11       F n-i-1 F n-i i       01 11       1 1 = n-2

21 Another program with O(log 2 n) (cont.)       F8F8 F9F9 =       01 11       1 1 = 7       01 11       1 1 3 2       01 11 =       01 11       1 1 2       01 11       01 11 2 =       01 11       1 1 2       11 12       01 11 =       01 11       1 1 2       12 23 =       01 11       58 813       1 1       8 21       1 1 ==       34

22 Thank You

23 Tower of Hanoi

24

25 Tower of Hanoi 時間複雜度分析 time complexity : O(2 n )


Download ppt "漫談 Fibonacci sequence 唐學明 2013.12.13.. Introduction Leonardo Pisano Bogollo, (c. 1170 – c. 1250) also known as Leonardo of Pisa, Leonardo Pisano, Leonardo."

Similar presentations


Ads by Google