Difference between revisions of "CSC111 Homework 9 Solutions"
(3 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
__TOC__ | __TOC__ | ||
− | + | <onlydft> | |
==Problem 1== | ==Problem 1== | ||
Line 99: | Line 99: | ||
# places by summing up terms of the form 1/n!. | # places by summing up terms of the form 1/n!. | ||
# | # | ||
+ | |||
+ | |||
sum = 1.0 | sum = 1.0 | ||
denom = 1 | denom = 1 | ||
count = 1 | count = 1 | ||
+ | series = "series = 1" | ||
print "-"*50 | print "-"*50 | ||
print "denom = ", denom | print "denom = ", denom | ||
− | print "term = ", 0 | + | #print "term = ", 0 |
− | print " | + | print series, " = ", sum |
− | print " | + | print "number of terms = ", count |
# 71828182845 | # 71828182845 | ||
Line 113: | Line 116: | ||
print "denom = ", denom | print "denom = ", denom | ||
term = 1.0 / denom | term = 1.0 / denom | ||
− | print "term = ", term | + | print "new term = ", term |
sum = sum + term | sum = sum + term | ||
− | |||
count += 1 | count += 1 | ||
+ | series = series + "+1/%d" % denom | ||
+ | print series | ||
+ | print "series = %1.20f" % sum | ||
+ | print "number of terms = ", count | ||
denom = denom * count | denom = denom * count | ||
− | |||
− | print "final sum = | + | print "final sum = ", sum |
print "count = ", count | print "count = ", count | ||
+ | |||
# the output of the program is shown below: | # the output of the program is shown below: | ||
Line 127: | Line 133: | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 1 | denom = 1 | ||
− | + | series = 1 = 1.0 | |
− | + | number of terms = 1 | |
− | |||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 1 | denom = 1 | ||
− | term = 1.0 | + | new term = 1.0 |
− | + | series = 1+1/1 | |
− | + | series = 2.00000000000000000000 | |
+ | number of terms = 2 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 2 | denom = 2 | ||
− | term = 0.5 | + | new term = 0.5 |
− | + | series = 1+1/1+1/2 | |
− | + | series = 2.50000000000000000000 | |
+ | number of terms = 3 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 6 | denom = 6 | ||
− | term = 0.166666666667 | + | new term = 0.166666666667 |
− | + | series = 1+1/1+1/2+1/6 | |
− | + | series = 2.66666666666666651864 | |
+ | number of terms = 4 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 24 | denom = 24 | ||
− | term = 0.0416666666667 | + | new term = 0.0416666666667 |
− | + | series = 1+1/1+1/2+1/6+1/24 | |
− | + | series = 2.70833333333333303727 | |
+ | number of terms = 5 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 120 | denom = 120 | ||
− | term = 0.00833333333333 | + | new term = 0.00833333333333 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120 | |
− | + | series = 2.71666666666666634100 | |
+ | number of terms = 6 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 720 | denom = 720 | ||
− | term = 0.00138888888889 | + | new term = 0.00138888888889 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720 | |
− | + | series = 2.71805555555555544700 | |
+ | number of terms = 7 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 5040 | denom = 5040 | ||
− | term = 0.000198412698413 | + | new term = 0.000198412698413 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040 | |
− | + | series = 2.71825396825396836675 | |
+ | number of terms = 8 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 40320 | denom = 40320 | ||
− | term = 2.48015873016e-05 | + | new term = 2.48015873016e-05 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320 | |
− | + | series = 2.71827876984127003723 | |
+ | number of terms = 9 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 362880 | denom = 362880 | ||
− | term = 2.7557319224e-06 | + | new term = 2.7557319224e-06 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880 | |
− | + | series = 2.71828152557319224769 | |
+ | number of terms = 10 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 3628800 | denom = 3628800 | ||
− | term = 2.7557319224e-07 | + | new term = 2.7557319224e-07 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800 | |
− | + | series = 2.71828180114638451315 | |
+ | number of terms = 11 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 39916800 | denom = 39916800 | ||
− | term = 2.50521083854e-08 | + | new term = 2.50521083854e-08 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800+1/39916800 | |
− | + | series = 2.71828182619849290091 | |
+ | number of terms = 12 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 479001600 | denom = 479001600 | ||
− | term = 2.08767569879e-09 | + | new term = 2.08767569879e-09 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800+1/39916800+1/479001600 | |
− | + | series = 2.71828182828616871092 | |
+ | number of terms = 13 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 6227020800 | denom = 6227020800 | ||
− | term = 1.60590438368e-10 | + | new term = 1.60590438368e-10 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800+1/39916800+1/479001600+1/6227020800 | |
− | + | series = 2.71828182844675936281 | |
+ | number of terms = 14 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 87178291200 | denom = 87178291200 | ||
− | term = 1.14707455977e-11 | + | new term = 1.14707455977e-11 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800+1/39916800+1/479001600+1/6227020800+1/87178291200 | |
− | + | series = 2.71828182845823018710 | |
− | final sum = 2. | + | number of terms = 15 |
+ | final sum = 2.71828182846 | ||
count = 15 | count = 15 | ||
Line 210: | Line 229: | ||
<br /> | <br /> | ||
− | == | + | ==Problem 3, Optional and Extra Credit== |
<source lang="python"> | <source lang="python"> | ||
Line 361: | Line 380: | ||
</source> | </source> | ||
− | + | </onlydft> | |
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 369: | Line 388: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | [[Category:CSC111]][[Category:Homework]] | + | [[Category:CSC111]][[Category:Homework]][[Category:Python]] |