Greatest common divisors & Fibonacci Numbers--Algorithms
来源:互联网 发布:淘宝搜同款 编辑:程序博客网 时间:2024/06/07 04:54
Greatest Common Divisors:
Naive Algorithm:
Function NaiveGCD(a,b)
best ←0
for dfrom1toa+b:
if d|aandd|b:
best←d
return best
Runtime approximatelya+b.
Very slow for 20 digit numbers.
Euclidean Algorithm
Function EuclidGCD(a,b)
if b=0:
return a
a′←the remainder whenais divided byb
return EuclidGCD(b,a′)
Each step reduces the size of numbersby about a factor of 2.
Takes about log(ab)steps.
GCDs of 100 digit numbers takes about 600 steps.
Each step a single division.
Fibonacci Number:
FibRecurs(n)
if n≤1:
return n
else:
return FibRecurs(n−1)+FibRecurs(n−2)
Too slow!
Better:
FibList(n)
create an array F[0...n]
F[0]←0
F[1]←1
for ifrom2ton:
F [i]←F[i−1] +F[i−2]
return F[n]
T(n)=2n+2. SoT(100)=202.
Easy to compute.
阅读全文
0 0
- Greatest common divisors & Fibonacci Numbers--Algorithms
- 4427. Greatest Common Divisors
- 4427. Greatest Common Divisors
- <OJ_Sicily>Greatest Common Divisors
- <Sicily>Greatest Common Divisors
- Sicily 4427. Greatest Common Divisors
- Sicily 4427. Greatest Common Divisors
- Greatest Common Divisor(GCD) of Two Numbers
- Algorithms - 最大公约数(greatest common divisor)-欧几里得(Euclid) 算法 及 代码
- Greatest Greatest Common Divisor
- Common Divisors
- Greatest Greatest Common Divisor HD5207
- Greatest common divisor
- The Greatest Common Divisor
- GCD (Greatest Common Divisor)
- Greatest Common Increasing Subsequence
- Solving Greatest Common Divisor
- the greatest common divisor
- 打开eclipse弹出Incompatible Java Version
- Linux: grep多个关键字“与”和“或”
- 【安全牛学习笔记】存储型XSS和BEEF浏览器攻击框架
- QT自定义弹框和输入框
- 欢迎使用CSDN-markdown编辑器
- Greatest common divisors & Fibonacci Numbers--Algorithms
- My97DatePicker日期插件
- 字体图标及“周边”
- 第五章引用类型
- android:kotlin语言开发再也不用findViewById与ButterKnife
- 算法马拉松29 美丽的集合(启发式合并/bitset)
- RSA加密
- 闭包的一个小demo
- 对c指针的理解解析