1 Star 0 Fork 1

Jeremy Lee/Python-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fibonacci.py 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
Jérôme Krell 提交于 2019-10-10 14:22 . Reformat Code by PyCharm-Community
# Fibonacci tool
# This script only works with Python3!
import time
def getFibonacciIterative(n: int) -> int:
"""
Calculate the fibonacci number at position n iteratively
"""
a = 0
b = 1
for i in range(n):
a, b = b, a + b
return a
def getFibonacciRecursive(n: int) -> int:
"""
Calculate the fibonacci number at position n recursively
"""
a = 0
b = 1
def step(n: int) -> int:
nonlocal a, b
if n <= 0:
return a
a, b = b, a + b
return step(n - 1)
return step(n)
def compareFibonacciCalculators(n: int) -> None:
"""
Interactively compare both fibonacci generators
"""
startI = time.clock()
resultI = getFibonacciIterative(n)
endI = time.clock()
startR = time.clock()
resultR = getFibonacciRecursive(n)
endR = time.clock()
s = "{} calculting {} => {} in {} seconds"
print(s.format(
"Iteratively", n, resultI, endI - startI
))
print(s.format(
"Recursively", n, resultR, endR - startR
))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Jelmy/Python-1.git
[email protected]:Jelmy/Python-1.git
Jelmy
Python-1
Python-1
master

搜索帮助