# 2.4.py # created on 21:02 2015-03-28 # $Id: 2.4.py 1.3 2016/11/13 06:27:20 s Exp $ from fractions import Fraction def f(x): return -1 + x + x**2 def fd(x): return 1 + 2 * x # main # calculate x0 = 1 x1 = x0 - f(x0) / fd(x0) x2 = x1 - f(x1) / fd(x1) x3 = x2 - f(x2) / fd(x2) x4 = x3 - f(x3) / fd(x3) # output print('2.4.py') print('in float') print('{:>10}{:>10}{:>10}{:>10}'.format('n', 'lambda(n)', 'f(lambda)', 'f\'(lambda)')) print(40 * '-') print('{:10d}{:10.7f}{:10.7f}{:10.7f}'.format(0, x0, f(x0), fd(x0))) print('{:10d}{:10.7f}{:10.7f}{:10.7f}'.format(1, x1, f(x1), fd(x1))) print('{:10d}{:10.7f}{:10.7f}{:10.7f}'.format(2, x2, f(x2), fd(x2))) print('{:10d}{:10.7f}{:10.7f}{:10.7f}'.format(3, x3, f(x3), fd(x3))) print('{:10d}{:10.7f}{:10.7f}{:10.7f}'.format(4, x4, f(x4), fd(x4))) print(40 * '-') # main #2 x0 = Fraction(1, 1) x1 = x0 - Fraction(f(x0), fd(x0)) x2 = x1 - Fraction(f(x1), fd(x1)) x3 = x2 - Fraction(f(x2), fd(x2)) x4 = x3 - Fraction(f(x3), fd(x3)) # output #2 fmt = '{:10d}{:21s}{:9.7f}' print() print('in fraction') print('{:>10}{:21}{}'.format('n', ' lambda(n)', 'lambda(n)')) print(40 * '-') print(fmt.format(0, ' ' + str(x0), float(x0))) print(fmt.format(1, ' ' + str(x1), float(x1))) print(fmt.format(2, ' ' + str(x2), float(x2))) print(fmt.format(3, ' ' + str(x3), float(x3))) print(fmt.format(4, ' ' + str(x4), float(x4))) print(40 * '-') # end of program # # below is output # # 2.4.py # in float # n lambda(n) f(lambda)f'(lambda) # ---------------------------------------- # 0 1.0000000 1.0000000 3.0000000 # 1 0.6666667 0.1111111 2.3333333 # 2 0.6190476 0.0022676 2.2380952 # 3 0.6180344 0.0000010 2.2360689 # 4 0.6180340 0.0000000 2.2360680 # ---------------------------------------- # # in fraction # n lambda(n) lambda(n) # ---------------------------------------- # 0 1 1.0000000 # 1 2/3 0.6666667 # 2 13/21 0.6190476 # 3 610/987 0.6180344 # 4 1346269/2178309 0.6180340 # ---------------------------------------- # end of output