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?
19 23636
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.. 
>>> sum(range(100))

4950

>>> 99*50

4950

>>>

>>> sum(range(100))/1000

4

>>> sum(range(100))/1000.0

4.9500000000000002

>>>
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: 
>>> sum(xrange(2**16))

2147450880

>>> sum(xrange(2**32))

File "<console>", line 1, in ?

''' exceptions.OverflowError : long int too large to convert to int '''

>>>
Infinite, huh? There's got to be some limit: 
>>> sum(xrange(2**16))

2147450880

>>> sum(xrange(2**32))

File "<console>", line 1, in ?

''' exceptions.OverflowError : long int too large to convert to int '''

>>>
yes, that why I limited k to 100
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.. 
>>> sum(range(100))

4950

>>> 99*50

4950

>>>

>>> sum(range(100))/1000

4

>>> sum(range(100))/1000.0

4.9500000000000002

>>>
it was not the code, only the problem because I could not type the mathematical symbol so I used Sum
yes, that why I limited k to 100
Now every one knows... I'm more of a mechanic than a mathematician.
here is what I want to calculate by using Python
here is what I want to calculate by using Python
This method comes up with the answer 2.71828182846. Is that right?  def factorial(n):

if n < 1:

return 1

else:

return n * factorial(n1)


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)
bvdet 2,851
Expert Mod 2GB
here is what I want to calculate by using Python
Does this work for you?  def factorial(n):

if n == 0:

return 1

else:

return n*factorial(n1)


def calc(k, u):

return float(sum(range(u+1)))/factorial(k)
>>> calc(4, 100)
210.41666666666666
>>>
This method comes up with the answer 2.71828182846. Is that right?  def factorial(n):

if n < 1:

return 1

else:

return n * factorial(n1)


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)
yes, it is e  thank you very much
Anyway, do we have another way (like using for loop) to approach this problem?
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. : 

>>> def factorial(n):

f=1

while(n>0):

f*=n # same as f=f*n, just faster

n=1# same as n=n1

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.7182818284590455


HTH
bvdet 2,851
Expert Mod 2GB
Now I understand the calculation you are after. Here is another version using a list comprehension:  >>> def calc(k, u):

... return sum([1.0/factorial(i) for i in range(k, u+1)])

...

>>> calc(0, 100)

2.7182818284590455

>>>
Now I understand the calculation you are after. Here is another version using a list comprehension:  >>> def calc(k, u):

... return sum([1.0/factorial(i) for i in range(k, u+1)])

...

>>> calc(0, 100)

2.7182818284590455

>>>
I tried code but it did not return any value.
bvdet 2,851
Expert Mod 2GB
I tried code but it did not return any value.
I tried it again:  >>> def calc(k, u):

... return sum([1.0/factorial(i) for i in range(k, u+1)])

...

>>> x = calc(50, 60)

>>> x

3.3536826466330641e065

>>>
What do you think is wrong with the code?

import operator

def factorial(x):

return reduce(operator.mul, xrange(2, x+1))

result = reduce(operator.add,[ factorial(x) for x in xrange(2,101) ])

print result

I intended to use only for loop (without using while loop, conditional statement, def) 
n=10

e=0

for k in range(1,n):

e=2+1.0/k*(k1)


print e


I got 2.88888888889 which wasn't closed to 2.7. What did I do wrong, how could I fix this without using while loop, conditional statement, def
I intended to use only for loop (without using while loop, conditional statement, def) 
n=10

e=0

for k in range(1,n):

e=2+1.0/k*(k1)


print e


I got 2.88888888889 which wasn't closed to 2.7. What did I do wrong
For one thing, you are assigning, not accumulating, as in:
But that won't make the formula that you have work.
I intended to use only for loop (without using while loop, conditional statement, def) 
n=10

e=0

for k in range(1,n):

e=2+1.0/k*(k1)


print e


I got 2.88888888889 which wasn't closed to 2.7. What did I do wrong, how could I fix this without using while loop, conditional statement, def
In your for loop, "k*(k1)" isn't equivalent to factorial. Every time it multiplies only two numbers together instead of the correct amount. As a result, the fractions being added together aren't getting smaller fast enough.
Is there any other way to accomplish this?
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
6 posts
views
Thread by Bengt Richter 
last post: by

5 posts
views
Thread by Yodai 
last post: by

reply
views
Thread by Travis Oliphant 
last post: by

2 posts
views
Thread by AWasilenko 
last post: by

45 posts
views
Thread by Carramba 
last post: by

reply
views
Thread by king kikapu 
last post: by

2 posts
views
Thread by mmiikkee13 
last post: by
            