Python includes exercises 24
Title: There is a sequence of scores: 2 / 1,3 / 2,5 / 3,8 / 5,13 / 8,21 / 13 and the front ... calculated the number of columns 20.
Program analysis: Hold the variation of the numerator and denominator.
Source Code:
method one:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 2.0 b = 1.0 s = 0 for n in range(1,21): s += a / b t = a a = a + b b = t print s
Method two:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 2.0 b = 1.0 s = 0.0 for n in range(1,21): s += a / b b,a = a , a + b print s s = 0.0 for n in range(1,21): s += a / b b,a = a , a + b print s
Method three:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 2.0 b = 1.0 l = [] for n in range(1,21): b,a = a,a + b l.append(a / b) print reduce(lambda x,y: x + y,l)
The above example output is:
32.6602607986