473,899 Members | 2,918 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
19 23878
python101
90 New Member
Anyway, do we have another way (like using for loop) to approach this problem?
Sep 21 '07 #11
kdt
50 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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 New Member
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 Recognized Expert Expert
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 New Member
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 New Member
Is there any other way to accomplish this?
Sep 25 '07 #20