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]
相关推荐
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-wordpress-theme-trunk"这个文件名中,"trunk"通常指的是主题的主分支,也就是开发人员进行持续更新和维护的地方。这可能意味着下载的版本包含了最新的开发版本,可能含有未发布的功能或改进,因此在...
通过深入研究 `prologue-examples-master` 中的示例,开发者不仅可以学习 Prologue 框架的用法,还能掌握 Nim 语言在 Web 开发中的最佳实践。这将有助于提升你的全栈开发技能,使你能够在高效、优雅的代码基础上构建...
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 ...
根据提供的文件信息,本书《Algorithms》是一本详细介绍算法理论与实践应用的书籍,由S. Dasgupta、C. H. Papadimitriou 和 U. V. Vazirani 共同编写,版权时间为2006年7月18日。下面将从各个章节入手,对书中所涉及...
《激光束通过随机介质传播第一章导言》是Larry C. Andrews所著的关于激光传输在随机介质(如大气)中传播的经典教材,这本教材共计18章,旨在为湍流大气通信学习者提供权威的指导。 首先,教材从光的历史背景和电磁...
"empty-prologue.github.io:我的六卦"是一个个人博客项目,托管在GitHub上,通过使用HTML(超文本标记语言)来构建网页内容。HTML是Web开发的基础,它定义了网页的结构和内容,使得文本、图像、视频等各种元素能够...
- `PROLOGUE`:开场部分,可能包含对C语言基础知识的回顾或引言。 - `NUMBERS`:与数字处理相关的题目,可能包括大整数运算、数字转换等。 - `SEARCH`:搜索算法的集合,如二分查找、广度优先搜索、深度优先搜索...
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服务的理想选择。 减少魔法。 减少惊喜。 当前工作现在,我们正在重写...
"prologue"在编程中通常指的是一个初始化阶段,用于设置环境或准备执行的代码。在MATLAB中,如果你希望在命令窗口(cmd窗口)中记录函数的执行过程,可以采用一些内置的输出函数或者自定义的日志系统。标题和描述中的...
关于开发环境 执行程序 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 ...
**Nim语言与Prologue命令工具详解** 在编程领域,Nim是一种静态类型的、系统级的、具有现代特性的编程语言,它强调简洁、高效和可移植性。Nim的设计目标是提供C++的性能,Python的易读性和Ruby的语法糖。在Nim的...
4. .prologue - 方法开始 5. .line - 方法位于 Java 代码的某一行 6. invoke-super - 调用父函数 7. const/high16 - 将常量赋值给寄存器 8. invoke-direct - 调用函数 9. return-void - 函数返回 void 10. .end ...
- **加密技术**:为了保障数据安全,卡片采用了一系列加密算法和技术来保护数据不被非法访问或篡改。 ##### 3. 物理接口要求 - **触点定义**:定义了智能卡与读卡器之间的物理连接方式,包括触点的位置、数量和...
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 ...
if-eqz v0, :cond_0 // 判断 v0 是否等于 0, 不符合条件向下走, 符合条件执行 cond_0 分支 .line 25 const/4 v1, 0x1 // 符合条件分支 .line 27 :goto_0 return v1 :cond_0 const/4 v1, 0x0 // cond_0 分支 ...