473,583 Members | 3,242 Online

# 'float' / 'int' object is not iterable

90 New Member
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?
Sep 19 '07 #1
19 23826
bartonc
6,596 Recognized Expert Expert
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..

Expand|Select|Wrap|Line Numbers
1. >>> sum(range(100))
2. 4950
3. >>> 99*50
4. 4950
5. >>>
6. >>> sum(range(100))/1000
7. 4
8. >>> sum(range(100))/1000.0
9. 4.9500000000000002
10. >>>
Sep 19 '07 #2
bartonc
6,596 Recognized Expert Expert
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:
Expand|Select|Wrap|Line Numbers
1. >>> sum(xrange(2**16))
2. 2147450880
3. >>> sum(xrange(2**32))
4.   File "<console>", line 1, in ?
5. ''' exceptions.OverflowError : long int too large to convert to int '''
6. >>>
Sep 19 '07 #3
python101
90 New Member
Infinite, huh? There's got to be some limit:
Expand|Select|Wrap|Line Numbers
1. >>> sum(xrange(2**16))
2. 2147450880
3. >>> sum(xrange(2**32))
4.   File "<console>", line 1, in ?
5. ''' exceptions.OverflowError : long int too large to convert to int '''
6. >>>
yes, that why I limited k to 100
Sep 20 '07 #4
python101
90 New Member
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..

Expand|Select|Wrap|Line Numbers
1. >>> sum(range(100))
2. 4950
3. >>> 99*50
4. 4950
5. >>>
6. >>> sum(range(100))/1000
7. 4
8. >>> sum(range(100))/1000.0
9. 4.9500000000000002
10. >>>
it was not the code, only the problem because I could not type the mathematical symbol so I used Sum
Sep 20 '07 #5
bartonc
6,596 Recognized Expert Expert
yes, that why I limited k to 100
Now every one knows... I'm more of a mechanic than a mathematician.
Sep 20 '07 #6
python101
90 New Member
here is what I want to calculate by using Python

Sep 21 '07 #7
KaezarRex
52 New Member
here is what I want to calculate by using Python

This method comes up with the answer 2.71828182846. Is that right?
Expand|Select|Wrap|Line Numbers
1. def factorial(n):
2.     if n < 1:
3.         return 1
4.     else:
5.         return n * factorial(n-1)
6.
7. def summation(k, limit):
8.     if k == limit:
9.         return 1.0/factorial(k)
10.     else:
11.         return 1.0/ factorial(k) + summation(k+1, limit)
12.
13. print summation(0, 100)
Sep 21 '07 #8
bvdet
2,851 Recognized Expert Moderator Specialist
here is what I want to calculate by using Python

Does this work for you?
Expand|Select|Wrap|Line Numbers
1. def factorial(n):
2.     if n == 0:
3.         return 1
4.     else:
5.         return n*factorial(n-1)
6.
7. def calc(k, u):
8.     return float(sum(range(u+1)))/factorial(k)
>>> calc(4, 100)
210.41666666666 666
>>>
Sep 21 '07 #9
python101
90 New Member
This method comes up with the answer 2.71828182846. Is that right?
Expand|Select|Wrap|Line Numbers
1. def factorial(n):
2.     if n < 1:
3.         return 1
4.     else:
5.         return n * factorial(n-1)
6.
7. def summation(k, limit):
8.     if k == limit:
9.         return 1.0/factorial(k)
10.     else:
11.         return 1.0/ factorial(k) + summation(k+1, limit)
12.
13. print summation(0, 100)
yes, it is e - thank you very much
Sep 21 '07 #10