Fibonacci numbers:
Introduction :
F(n) = F(n-1) + F(n-2) , for n >= 2, and F(0) = 0, F(1) = 1
One approximation of F(n) is roughly , now I'm gonna prove it first.
Promble: Find a constant c < 1 such that F(n) <= for all n >= 0 , and find the minimal c.
proof : We use induction to prove it and intuitively we can guess what is c.
Suppose F(n) <= , then we need to show F(n+1) <= which means that + <= . Solve this inequlity we get c >= , the following steps is simple, I'll leave it out.
Then what does this approximation means? It means that the bit-length of F(n) is roughly 0.694n. So for a 32-bit machine, the word size is far less than F(n) , the arithmetic operation on F(n) cann't be considered as O(1) time given the bit-length.
These are three ways to calculate Fibonacci:
1. An exponential algorithm:
def fib1(n): """ This function calculates fibonacci series. It takes exponential time. """ if n <= 0 : return 0 elif n == 1 : return 1 else: return fib1(n-1) + fib1(n-2)
Running time analysis: T(n) = T(n-1) + T(n-2) + 3 , for n > 1. obviously T(n) >= F(n) which grows exponentially.
2. a Polynomial algorithm
def fib2(n): """ This function calculates fibonacci series. It takes linear time. If n is too large, it takes roughly quadratic time. """ if n == 0 : return 0 res = [] res.append(0) res.append(1) for i in range(2,n+1): res.append(res[i-1] + res[i-2]) return res[n]
Running time analysis: it's easy to see that it has reduced many redundant recurrences, the running time depends on the execution time of (res[i-1] + res[i-2]).
if res[i] is within machine word-length, it takes O(1) time, and the total time is O(n)
But if res[i] is beyond machine word-length, it takes O( ) time and the total time is O(n* ) = O( ) which is polynomial to n.
3. A log time algorithm
import numpy as np def quick_power(a,n): """ This function calculates a to the power of n. It takes log time. 未处理0的情况 """ if n == 1: return a elif n== 2: return a * a elif n % 2 == 0: temp_res = quick_power(a, n/2) return temp_res * temp_res else: temp_res = quick_power(a, (n-1)/2) return temp_res * temp_res * a def fib3(n): """ This function calculates fibonacci series. It takes log time. """ origin = np.matrix([[1,1], [1,0]]) res = quick_power(origin, n) return res[0, 1]
Running time analysis: This algorithm use an fact that :
We use quick_power to do n-power operation for O(log n) time, but there are some problem in it. We do 8 mulplications in matrix mulplication rather than simple addition. As you know, multiplication may take more time than addition, so when n is large , it needs further analysis. This part isn't completed.
Finally this is the running result of above 3 algorithms: Red : fib1 , blue : fib2, grenn : fib3 , y-axis: time, x-axis : n.
- [list=1][list=1][list=1]
[/list]
[/list]
[/list]
相关推荐
0Prologue(序论) 0.1Booksandalgorithms(书和算法) 0.2EnterFibonacci(斐波那契数列) 0.3Big-Onotation(大O记号) Exercises(习题) 1Algorithmswithnumbers(数的算法) 1.1Basicarithmetic(基本算术...
Chapter 0 Prologue Chapter 1 Algorithms with numbers Chapter 2 Divide-and-conquer algorithms Chapter 3 Decompositions of graphs Chapter 4 Paths in graphs Chapter 5 Greedy algorithms Chapter 6 Dynamic ...
Chapter 0 Prologue Chapter 1 Algorithms with numbers Chapter 2 Divide-and-conquer algorithms Chapter 3 Decompositions of graphs Chapter 4 Paths in graphs Chapter 5 Greedy algorithms Chapter 6 Dynamic ...
这是Prologue,它是一个简单的,单页面响应式网站模板,现在可以从作为博客意识的Jekyll主题使用。 它具有简洁,简约的设计以及带有导航链接滚动功能的粘边栏。 演示: : 新增功能 您希望Jekyll提供博客和多页...
prologue-examples:存放以Nim语言编写的Prologue框架的示例的存储库
序言WordPress主题序幕WordPress主题
Prologue Chapter 1. The Python Data Model Part II. Data structures Chapter 2. An array of sequences Chapter 3. Dictionaries and sets Chapter 4. Text versus bytes Part III. Functions as objects ...
empty-prologue.github.io:我的六卦
by Andrews, Larry C. 随机介质光传输领域最经典的权威教材,适用于湍流大气通信学习,共18章
gatsby-starter-prologue 基于Prologue by HTML5 UP的Gatsby.js V2入门模板 有关项目结构的概述,请参阅。 查看在线预览 截屏 安装 确保已安装Gatsby CLI程序: npm install --global gatsby-cli 并从您的CLI...
Prologue是用Python编写的可扩展文本预处理器。 它以连续流的形式执行评估,从而使其能够快速运行,并在几乎没有打开文件句柄的情况下保持最小的内存占用。 可以轻松添加和删除指令以自定义预处理器的行为。 默认...
Prologue是一个全栈Web框架,是构建优雅,高性能Web服务的理想选择。 序幕过去是序幕。 目的Prologue是一个全栈Web框架,是构建优雅,高性能Web服务的理想选择。 减少魔法。 减少惊喜。 当前工作现在,我们正在重写...
关于开发环境 执行程序 docker-compose build docker-compose run --rm front sh -c "yarn upgrade" docker-compose run api sh -c "rails db:create" docker-compose up 删除容器 docker-compose down ...
foo()函数代码如下: # virtual methods .method public foo(II)I .registers 5 .prologue .line 3 add-int v0, p1, p2 sub-int v1, p1, p2 mul-int/2addr v0, v1 return v0 .end method java -jar ddx.jar -d ...
Laravel开发-alerts Prologue Alerts是一个处理全局站点消息的包。
将此行插入到函数开头,然后每次执行都将记录在cmd窗口中 序幕;
Function prologue and epilogue 308 Defining local function data 309 Cleaning out the stack 312 An example 312 Watching the stack in action 314 Using Separate Function Files 317 Creating a separate ...
logue:Nim中Prologue的命令工具
Prologue: Starting With A Handshake Photos Part I: The Promise Of Social Business 1 A New Kind of Business Is Government the Answer? The Contribution of Nonprofit Organizations Multilateral ...
DS_prologue 数据科学的序幕