'float' / 'int' object is not iterable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    'float' / 'int' object is not iterable

    I try to calculate the sum of an infinite series
    S = Sum (from n=0 to 100) 1/k!
    I got message 'float' / 'int' object is not iterable. How can I fix this?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by python101
    I try to calculate the sum of an infinite series
    S = Sum (from n=0 to 100) 1/k!
    I got message 'float' / 'int' object is not iterable. How can I fix this?
    Hmmm... I've never seen syntax like that before.. The capital "S" in "Sum" should have thrown the first error in pure Python. ...Then there is the lack of CODE tags in your post..

    [CODE=python]
    >>> sum(range(100))
    4950
    >>> 99*50
    4950
    >>>
    >>> sum(range(100))/1000
    4
    >>> sum(range(100))/1000.0
    4.9500000000000 002
    >>> [/CODE]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by python101
      I try to calculate the sum of an infinite series
      S = Sum (from n=0 to 100) 1/k!
      I got message 'float' / 'int' object is not iterable. How can I fix this?
      Infinite, huh? There's got to be some limit:[CODE=python]
      >>> sum(xrange(2**1 6))
      2147450880
      >>> sum(xrange(2**3 2))
      File "<console>" , line 1, in ?
      ''' exceptions.Over flowError : long int too large to convert to int '''
      >>> [/CODE]

      Comment

      • python101
        New Member
        • Sep 2007
        • 90

        #4
        Originally posted by bartonc
        Infinite, huh? There's got to be some limit:[CODE=python]
        >>> sum(xrange(2**1 6))
        2147450880
        >>> sum(xrange(2**3 2))
        File "<console>" , line 1, in ?
        ''' exceptions.Over flowError : long int too large to convert to int '''
        >>> [/CODE]
        yes, that why I limited k to 100

        Comment

        • python101
          New Member
          • Sep 2007
          • 90

          #5
          Originally posted by bartonc
          Hmmm... I've never seen syntax like that before.. The capital "S" in "Sum" should have thrown the first error in pure Python. ...Then there is the lack of CODE tags in your post..

          [CODE=python]
          >>> sum(range(100))
          4950
          >>> 99*50
          4950
          >>>
          >>> sum(range(100))/1000
          4
          >>> sum(range(100))/1000.0
          4.9500000000000 002
          >>> [/CODE]
          it was not the code, only the problem because I could not type the mathematical symbol so I used Sum

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by python101
            yes, that why I limited k to 100
            Now every one knows... I'm more of a mechanic than a mathematician.

            Comment

            • python101
              New Member
              • Sep 2007
              • 90

              #7
              here is what I want to calculate by using Python

              Comment

              • KaezarRex
                New Member
                • Sep 2007
                • 52

                #8
                Originally posted by python101
                here is what I want to calculate by using Python

                This method comes up with the answer 2.71828182846. Is that right?
                [CODE=python]def factorial(n):
                if n < 1:
                return 1
                else:
                return n * factorial(n-1)

                def summation(k, limit):
                if k == limit:
                return 1.0/factorial(k)
                else:
                return 1.0/ factorial(k) + summation(k+1, limit)

                print summation(0, 100)[/CODE]

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Originally posted by python101
                  here is what I want to calculate by using Python
                  Does this work for you?[code=Python]def factorial(n):
                  if n == 0:
                  return 1
                  else:
                  return n*factorial(n-1)

                  def calc(k, u):
                  return float(sum(range (u+1)))/factorial(k)[/code]

                  >>> calc(4, 100)
                  210.41666666666 666
                  >>>

                  Comment

                  • python101
                    New Member
                    • Sep 2007
                    • 90

                    #10
                    Originally posted by KaezarRex
                    This method comes up with the answer 2.71828182846. Is that right?
                    [CODE=python]def factorial(n):
                    if n < 1:
                    return 1
                    else:
                    return n * factorial(n-1)

                    def summation(k, limit):
                    if k == limit:
                    return 1.0/factorial(k)
                    else:
                    return 1.0/ factorial(k) + summation(k+1, limit)

                    print summation(0, 100)[/CODE]
                    yes, it is e - thank you very much

                    Comment

                    • python101
                      New Member
                      • Sep 2007
                      • 90

                      #11
                      Anyway, do we have another way (like using for loop) to approach this problem?

                      Comment

                      • kdt
                        New Member
                        • Mar 2007
                        • 50

                        #12
                        Originally posted by python101
                        Anyway, do we have another way (like using for loop) to approach this problem?
                        If you want to avoid the use of recursion you can use the following. The factorial is calculated using a while loop, and the summation by a for loop. :

                        [CODE=python]

                        >>> def factorial(n):
                        f=1
                        while(n>0):
                        f*=n # same as f=f*n, just faster
                        n-=1# same as n=n-1
                        return f

                        >>> def summation(k, limit):
                        sigma=0
                        for i in range(k, limit+1): # +1 added to catch both k=0 and k=100
                        sigma+=(1.0/factorial(i))
                        return sigma

                        >>> summation(0, 100)
                        2.7182818284590 455

                        [/CODE]

                        HTH

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          Now I understand the calculation you are after. Here is another version using a list comprehension:[code=Python]>>> def calc(k, u):
                          ... return sum([1.0/factorial(i) for i in range(k, u+1)])
                          ...
                          >>> calc(0, 100)
                          2.7182818284590 455
                          >>> [/code]

                          Comment

                          • python101
                            New Member
                            • Sep 2007
                            • 90

                            #14
                            Originally posted by bvdet
                            Now I understand the calculation you are after. Here is another version using a list comprehension:[code=Python]>>> def calc(k, u):
                            ... return sum([1.0/factorial(i) for i in range(k, u+1)])
                            ...
                            >>> calc(0, 100)
                            2.7182818284590 455
                            >>> [/code]
                            I tried code but it did not return any value.

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              Originally posted by python101
                              I tried code but it did not return any value.
                              I tried it again:[code=Python]>>> def calc(k, u):
                              ... return sum([1.0/factorial(i) for i in range(k, u+1)])
                              ...
                              >>> x = calc(50, 60)
                              >>> x
                              3.3536826466330 641e-065
                              >>> [/code]What do you think is wrong with the code?

                              Comment

                              Working...