Skip to content

Instantly share code, notes, and snippets.

@tommyct614
Created January 28, 2018 18:09
Show Gist options
  • Save tommyct614/91d3d735c4be57ab8075d2f1ba8da7b1 to your computer and use it in GitHub Desktop.
Save tommyct614/91d3d735c4be57ab8075d2f1ba8da7b1 to your computer and use it in GitHub Desktop.
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