Difference between revisions of "CSC111 Homework 9 Solutions"
(Created page with '<onlydft> ==Problem 2== <source lang="python"> sum = 1.0 denom = 1 count = 1 print "sum = ", sum # 71828182845 while sum*100000000000 < 271828182845: …') |
|||
Line 7: | Line 7: | ||
denom = 1 | denom = 1 | ||
count = 1 | count = 1 | ||
+ | print "-"*50 | ||
+ | print "denom = ", denom | ||
+ | print "term = ", 0 | ||
+ | print "sum = ", sum | ||
+ | print "count = ", count | ||
− | |||
# 71828182845 | # 71828182845 | ||
while sum*100000000000 < 271828182845: | while sum*100000000000 < 271828182845: | ||
Line 16: | Line 20: | ||
print "term = ", term | print "term = ", term | ||
sum = sum + term | sum = sum + term | ||
− | print "sum = " | + | print "sum = %1.20f" % sum |
count += 1 | count += 1 | ||
denom = denom * count | denom = denom * count | ||
+ | print "count = ", count | ||
− | print "final sum = " | + | print "final sum = %1.20f " % sum |
print "count = ", count | print "count = ", count | ||
− | |||
# the output of the program is shown below: | # the output of the program is shown below: | ||
""" | """ | ||
+ | -------------------------------------------------- | ||
+ | denom = 1 | ||
+ | term = 0 | ||
sum = 1.0 | sum = 1.0 | ||
+ | count = 1 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 1 | denom = 1 | ||
term = 1.0 | term = 1.0 | ||
− | sum = 2 | + | sum = 2.00000000000000000000 |
+ | count = 2 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 2 | denom = 2 | ||
term = 0.5 | term = 0.5 | ||
− | sum = | + | sum = 2.50000000000000000000 |
+ | count = 3 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 6 | denom = 6 | ||
term = 0.166666666667 | term = 0.166666666667 | ||
− | sum = | + | sum = 2.66666666666666651864 |
+ | count = 4 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 24 | denom = 24 | ||
term = 0.0416666666667 | term = 0.0416666666667 | ||
− | sum = | + | sum = 2.70833333333333303727 |
+ | count = 5 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 120 | denom = 120 | ||
term = 0.00833333333333 | term = 0.00833333333333 | ||
− | sum = | + | sum = 2.71666666666666634100 |
+ | count = 6 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 720 | denom = 720 | ||
term = 0.00138888888889 | term = 0.00138888888889 | ||
− | sum = | + | sum = 2.71805555555555544700 |
+ | count = 7 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 5040 | denom = 5040 | ||
term = 0.000198412698413 | term = 0.000198412698413 | ||
− | sum = | + | sum = 2.71825396825396836675 |
+ | count = 8 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 40320 | denom = 40320 | ||
term = 2.48015873016e-05 | term = 2.48015873016e-05 | ||
− | sum = | + | sum = 2.71827876984127003723 |
+ | count = 9 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 362880 | denom = 362880 | ||
term = 2.7557319224e-06 | term = 2.7557319224e-06 | ||
− | sum = | + | sum = 2.71828152557319224769 |
+ | count = 10 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 3628800 | denom = 3628800 | ||
term = 2.7557319224e-07 | term = 2.7557319224e-07 | ||
− | sum = | + | sum = 2.71828180114638451315 |
+ | count = 11 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 39916800 | denom = 39916800 | ||
term = 2.50521083854e-08 | term = 2.50521083854e-08 | ||
− | sum = | + | sum = 2.71828182619849290091 |
+ | count = 12 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 479001600 | denom = 479001600 | ||
term = 2.08767569879e-09 | term = 2.08767569879e-09 | ||
− | sum = | + | sum = 2.71828182828616871092 |
+ | count = 13 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 6227020800 | denom = 6227020800 | ||
term = 1.60590438368e-10 | term = 1.60590438368e-10 | ||
− | sum = | + | sum = 2.71828182844675936281 |
+ | count = 14 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 87178291200 | denom = 87178291200 | ||
term = 1.14707455977e-11 | term = 1.14707455977e-11 | ||
− | sum = | + | sum = 2.71828182845823018710 |
− | final sum = 2. | + | count = 15 |
+ | final sum = 2.71828182845823018710 | ||
count = 15 | count = 15 | ||
+ | |||
""" | """ | ||