How to get an approx sum of an infinite series in python.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuugie
    New Member
    • Sep 2010
    • 32

    How to get an approx sum of an infinite series in python.

    I'm having trouble writing a program that will get me an approximate sum of an infinite series.

    This is my code:

    Code:
    def factorial(n):
         if n == 0:
             return 1
         else:
             return n * factorial(n-1)
    
    if 
    
    print
    raw_input()
    This is the equation i have to use:


    How would I get this to finish solving for 'e' ?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You got the factorial part. I am using a list comprehension in the code below to determine the value of e.
    Code:
    >>> def factorial(n):
    ...     if n == 0:
    ...         return 1
    ...     return n*factorial(n-1)
    ... 
    >>> def calc(u):
    ...     return sum([1.0/factorial(i) for i in range(0, u+1)])
    ... 
    >>> calc(100)
    2.7182818284590455
    >>>

    Comment

    • Fuugie
      New Member
      • Sep 2010
      • 32

      #3
      how would i have it print the solution afterwards?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        If you are using Python 2.X:
        Code:
        print calc(number)
        Perhaps you should spend some time perusing a Python beginner's guide.

        Comment

        • Fuugie
          New Member
          • Sep 2010
          • 32

          #5
          I didn't mean like that, i know how to get something to print. I was asking about the solution to the problem. I still can't get this to work correctly.

          Here is my code:
          Code:
          terms = raw_input('How many terms? ')
          
          def factorial(n):
               if n == 0:
                   return 1
               else:
                   return n * factorial(n-1)
               
          def calu(u):
               return sum([1.0/factorial(n) for i in range(0, u + 1)])
          
          
          print (calu)
          raw_input()

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Try this:
            Code:
            value = calu(int(terms))
            print (value)
            You also have a problem in calu(). Variable n should be variable i.

            I recommend you study this documentation on Python scope rules.

            Comment

            • Fuugie
              New Member
              • Sep 2010
              • 32

              #7
              Why does the variable have to be i instead of n?

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Variable n is local to the scope of function factorial() and is not defined in the scope of calu(). In addition to that, look at the following:
                Code:
                >>> sum([i for i in range(5)])
                10
                >>> n = 100
                >>> sum([n for i in range(5)])
                500
                >>> range(5)
                [0, 1, 2, 3, 4]
                >>>
                As I stated in my previous post: I recommend you study this documentation on Python scope rules.

                Comment

                • Fuugie
                  New Member
                  • Sep 2010
                  • 32

                  #9
                  Alright. Thank you so much for the help bvdet!

                  Comment

                  Working...