Created
January 28, 2018 18:09
-
-
Save tommyct614/91d3d735c4be57ab8075d2f1ba8da7b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as numpy | |
"""This program takes a series | |
and checks for convergence value | |
of 1/n!""" | |
currSum = 1.0 | |
t = 1e-3 # tolerance | |
prevSum = 0 | |
perDiff = 1.0 | |
n = 0 | |
seriesTerm = 1.0 | |
while perDiff > t: | |
n += 1 | |
prevSum = currSum | |
seriesTerm *= 1/n # This saves the individual | |
#factorial value terms. | |
currSum += seriesTerm # This adds them up. | |
perDiff = abs(currSum-prevSum)/currSum | |
print(n, seriesTerm, currSum, perDiff) | |
print("The series converges to ", currSum, " at the ", n, "th iteration.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment