472,143 Members | 1,375 Online

# 'float' / 'int' object is not iterable 90
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 23636 bartonc
6,596 Expert 4TB
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 Expert 4TB
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 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 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 Expert 4TB
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 here is what I want to calculate by using Python Sep 21 '07 #7
KaezarRex
52 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 Expert Mod 2GB
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.41666666666666
>>>
Sep 21 '07 #9
python101
90 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
python101
90 Anyway, do we have another way (like using for loop) to approach this problem?
Sep 21 '07 #11
kdt
50 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. :

Expand|Select|Wrap|Line Numbers
1.
2. >>> def factorial(n):
3.     f=1
4.     while(n>0):
5.         f*=n # same as f=f*n, just faster
6.         n-=1# same as n=n-1
7.     return f
8.
9. >>> def summation(k, limit):
10.     sigma=0
11.     for i in range(k, limit+1): # +1 added to catch both k=0 and k=100
12.         sigma+=(1.0/factorial(i))
13.     return sigma
14.
15. >>> summation(0, 100)
16. 2.7182818284590455
17.
18.
HTH
Sep 22 '07 #12
bvdet
2,851 Expert Mod 2GB
Now I understand the calculation you are after. Here is another version using a list comprehension:
Expand|Select|Wrap|Line Numbers
1. >>> def calc(k, u):
2. ...     return sum([1.0/factorial(i) for i in range(k, u+1)])
3. ...
4. >>> calc(0, 100)
5. 2.7182818284590455
6. >>>
Sep 22 '07 #13
python101
90 Now I understand the calculation you are after. Here is another version using a list comprehension:
Expand|Select|Wrap|Line Numbers
1. >>> def calc(k, u):
2. ...     return sum([1.0/factorial(i) for i in range(k, u+1)])
3. ...
4. >>> calc(0, 100)
5. 2.7182818284590455
6. >>>
I tried code but it did not return any value.
Sep 24 '07 #14
bvdet
2,851 Expert Mod 2GB
I tried code but it did not return any value.
I tried it again:
Expand|Select|Wrap|Line Numbers
1. >>> def calc(k, u):
2. ...     return sum([1.0/factorial(i) for i in range(k, u+1)])
3. ...
4. >>> x = calc(50, 60)
5. >>> x
6. 3.3536826466330641e-065
7. >>>
What do you think is wrong with the code?
Sep 25 '07 #15
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
1. import operator
2. def factorial(x):
3.    return reduce(operator.mul, xrange(2, x+1))
4. result = reduce(operator.add,[ factorial(x) for x in xrange(2,101) ])
5. print result
6.
Sep 25 '07 #16
python101
90 I intended to use only for loop (without using while loop, conditional statement, def)

Expand|Select|Wrap|Line Numbers
1. n=10
2. e=0
3. for k in range(1,n):
4.         e=2+1.0/k*(k-1)
5.
6. print e
7.
8.
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
Sep 25 '07 #17
bartonc
6,596 Expert 4TB
I intended to use only for loop (without using while loop, conditional statement, def)

Expand|Select|Wrap|Line Numbers
1. n=10
2. e=0
3. for k in range(1,n):
4.         e=2+1.0/k*(k-1)
5.
6. print e
7.
8.
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:
Expand|Select|Wrap|Line Numbers
1. #
2.         e += 2+1.0/k*(k-1)
But that won't make the formula that you have work.
Sep 25 '07 #18
KaezarRex
52 I intended to use only for loop (without using while loop, conditional statement, def)

Expand|Select|Wrap|Line Numbers
1. n=10
2. e=0
3. for k in range(1,n):
4.         e=2+1.0/k*(k-1)
5.
6. print e
7.
8.
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*(k-1)" 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.
Sep 25 '07 #19
python101
90 Is there any other way to accomplish this?
Sep 25 '07 #20

 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 5 posts views Thread by cj007541 | last post: by 4 posts views Thread by arturo trafny | last post: by reply views Thread by beacampos | last post: by reply views Thread by isladogs | last post: by reply views Thread by antdb | last post: by reply views Thread by pddon | last post: by reply views Thread by antdb | last post: by reply views Thread by antdb | last post: by 3 posts views Thread by bobbyer | last post: by 7 posts views Thread by bounthong | last post: by 1 post views Thread by Computer0300 | last post: by