473,899 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'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

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2097
by: Bengt Richter | last post by:
Peculiar boundary cases: >>> 2.0**31-1.0 2147483647.0 >>> int(2147483647.0) 2147483647L >>> int(2147483647L ) 2147483647 >>> >>> -2.0**31
5
4551
by: Yodai | last post by:
Hi all! I have an int that comes with a value like 0x07fa and I have to turn it into a float value of 204.2 decimal to display it.... if I try to divide it by 10 I get bogus numbers. I presume I have to make it decimal before calculating, but I am not sure about it. Any light upon this simple (or it seems to me so, at least) begginner problem? TIA..
0
1384
by: Travis Oliphant | last post by:
This post is to gather feedback from the wider community on PEP 357. It is nearing the acceptance stage and has previously been discussed on python-dev. This is a chance for the wider Python community to comment on the proposal. You will find the PEP attached PEP: 357
2
6761
by: AWasilenko | last post by:
I'm trying to test a few different approaches to displaying pages via Cherrypy and I'm not having much luck. Here is my code so far: import sys, cherrypy, html class Root: @cherrypy.expose def index(self, pageid = None): selection = html.Page() return selection.input()
45
5335
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can multiple it by 1000 to get 11111 and the preform bitwise like <<2 to get 88888 and then divide by 1000 to go back to float 8.8888. but these seem like "nasty" way to do it. So maybe some of you have great tips? Thank you in advance! L R
0
1408
by: king kikapu | last post by:
Hi, i have a problem with the following piece of code that id just drive me nuts (from the morning...) I think is more Python specific than Qt, folks from Qt forum have already give me directions of how to do it but that Python error message is just impossible for me to figure out. And i am sure that it something just under my nose... So, i have a form with 2 spin boxes, a button and a progress bar. When
2
11016
by: mmiikkee13 | last post by:
>>a_list = range(37) .... print k, v .... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable What 'int' object is this referring to? I'm iterating over a dict, not an int.
5
9832
by: cj007541 | last post by:
Hey. Im working on this for a school project thing and i need some help please. from Tkinter import* import random import time number = 5 i = 0
4
4085
by: arturo trafny | last post by:
I am getting the following error: ##this is the error message i receive Traceback (most recent call last): File "C:\Users\a\Desktop\checkerboard", line 26, in <module> print(checkerboard(plaintext)) File "C:\Users\a\Desktop\checkerboard", line 20, in checkerboard ciphertext=ciphertext+idx TypeError: Can't convert 'int' object to str implicitly
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10866
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8043
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5891
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6082
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.