""" 题目:斐波那契数列。 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……。 这里所有的斐波那契数列都是以0开头来算 """ import functools def answer1(): """ 输出100000以内斐波那契数列 :return: """ print("\n输出一", end=":") a = 0 print(a, end=",") b = 1 print(b, end=",") c = a + b while c < 100000: print(c, end=",") a = b b = c c = a + b answer1() def answer2(): """ 参考答案用到了多变量赋值 :return: """ print("\n输出二", end=":") a, b = 0, 1 print(a, end=",") while b < 100000: print(b, end=",") a, b = b, a + b answer2() def answer3(n, m): """ 利用递归来实现 :param n: :return: """ if n == 0: print("\n输出三", end=":") print(n, end=",") if m < 100000: print(m, end=",") n, m = m, n + m answer3(n, m) else: return answer3(0, 1) def answer4(): """ 利用代数公式计算,公式可参考example006_FormulaDerivation文件: F(n)=(√5/5)*{[(1+√5)/2]^n - [(1-√5)/2]^n}。 :return: """ print("\n输出四", end=":") i = 0 n = 0 while n < 100000: print(int(n), end=",") i += 1 n = (5 ** 0.5 / 5) * (((1 + 5 ** 0.5) / 2) ** i - ((1 - 5 ** 0.5) / 2) ** i) answer4() def answer5(n): """ 根据输入整数n,获取前n个斐波那契数 :param n: :return: """ print("\n输出五", end=":") a = 0 print(a, end=",") b = 1 for i in range(1, n): print(b, end=",") a, b = b, a + b answer5(26) def answer6(n): """ 利用另一种递归来计算,注意与answer3(n, m)区分 其中用到了内嵌函数 :param n: :return: """ print("\n输出六", end=":") def getOne(index): if index == 1: return 0 if index == 2: return 1 num = getOne(index - 2) + getOne(index - 1) return num for i in range(1, n+1): print(getOne(i), end=",") answer6(26) def answer7(n): """ 利用数组来计算 :param n: :return: """ print("\n输出七", end=":") num = [0, 1] for i in range(2, n): num.append(num[-1] + num[-2]) print(num) answer7(26) def answer8(n): """ 利用公司算前n个 :param n: :return: """ print("输出八", end=":") for i in range(0, n): num = (5 ** 0.5 / 5) * (((1 + 5 ** 0.5) / 2) ** i - ((1 - 5 ** 0.5) / 2) ** i) print(int(num), end=",") answer8(26) def answer9(n): """ 多变量赋值,获取前n个斐波那契数列 :param n: :return: """ print("\n输出九", end=":") a, b = 0, 1 while n: print(a, end=",") a, b, n = b, a + b, n - 1 answer9(26) @functools.lru_cache(None) def answer10(n): """ 在 Python 的 3.2 版本中,引入了一个非常优雅的缓存机器,即 functool 模块中的 lru_cache 装饰器。如果要在 python2 中使用 lru_cahce 需要安装 functools32。lru_cache 原型如下: @functools.lru_cache(maxsize=None, typed=False) 使用functools模块的lur_cache装饰器,可以缓存最多 maxsize 个此函数的调用结果,从而提高程序执行的效率,特别适合于耗时的函数。参数maxsize为最多缓存的次数,如果为None,则无限制,设置为2n时,性能最佳;如果 typed=True(注意,在 functools32 中没有此参数),则不同参数类型的调用将分别缓存,例如 f(3) 和 f(3.0)。 被 lru_cache 装饰的函数会有 cache_clear 和 cache_info 两个方法,分别用于清除缓存和查看缓存信息。 这里用一个简单的示例演示 lru_cache 效果: from functools import lru_cache @lru_cache(None) def add(x, y): print("calculating: %s + %s" % (x, y)) return x + y print(add(1, 2)) print(add(1, 2)) print(add(2, 3)) 输出结果: calculating: 1 + 2 3 3 calculating: 2 + 3 5 从结果可以看出,当第二次调用 add(1, 2) 时,并没有真正执行函数体,而是直接返回缓存的结果。 有一个用 C 实现的,更快的,同时兼容 Python2 和 Python3 的第三方模块 fastcache 能够实现同样的功能。 :param n: :return: """ if n == 1: print("\n输出十", end=":") assert n >= 1 m = 0 if n == 1 else answer10(1)+1 if n == 2 else answer10(n - 2) + answer10(n - 1) print(m, end=",") return m answer10(26) def answer11(n): """ reduce练习 :return: """ print("\n输出十一", end=":") num = [0, 1] for i in range(2, n): num.append(functools.reduce(lambda x, y: x + y, num[-1: -3: -1])) print(num) answer11(26) def answer12(n): """ 练习sum 和列表切片 :param n: :return: """ print("输出十二", end=":") num = [0, 1] for i in range(2, n): num.append(sum(num[i-2: i])) print(num) answer12(26) class Fibs: """ 练习类,创建一个迭代器 """ def __init__(self, n): self.a = 0 self.b = 1 self.n = n self.i = 1 print("输出十三", end=":") def __iter__(self): return self def __next__(self): if self.i > self.n: raise StopIteration print(self.a, end=",") self.a, self.b = self.b, self.a + self.b self.i += 1 f = Fibs(26) for i in f: pass