473,781 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

composites numbers newbie asking for help!! PLEASE!!!

6 New Member
Hi,

i am in desperate need for any help regarding one of my assignments. I am to write a python program that lists the numbers that are composite from 1 to n(input) and write it to an external txt. I was able to write something that checks whether something is composite or not but able to incorporate it into a loop as such.

for example: n = 50

then the composite numbers are

4
6
8
10
etc...

Expand|Select|Wrap|Line Numbers
  1. n = input ("Please enter a number greater than 1 (0 for exit):")
  2. if n == 0:
  3.     print exit()
  4. def get_factors(n):
  5.     List1 = []
  6.     List2 = []
  7.     List3 = []
  8.     for x in range(2, int(n**0.5)+1):
  9.         if n % x == 0:
  10.             List1 = List1 + [x]
  11.             for y in List1:
  12.                 c = n / y
  13.                 if c not in List2: 
  14.                     List2 = List2 + [c]
  15.     List3 = List1 + List2
  16.     List3.sort()
  17.     return List3
  18.  
  19. #see if number is a prime
  20. def prime(n):
  21.  
  22.     #range starts with 2 and only needs to go up the squareroot of n
  23.     for x in range(2, int(n**0.5)+1):  
  24.         if n % x == 0:
  25.             return False
  26.     return True
  27.  
  28.  
  29. #print whther its a prime or not
  30. if prime(n):
  31.     print n, "is a not a composite number"
  32.  
  33. else:
  34.     print n, "is a composite number!"
  35.     print get_factors(n)
  36.  
  37.  
  38. #program 2   
  39. n = input("Please enter a number greater than 2: ")
  40. outfile = open('primes-'+str(n)+'.txt','w')
  41.  
  42.  
  43. outfile.write("prime(n)" + '\n')
  44.  
  45. print "Found", len(prime_number(n)), "numbers; please check file primes-",n,".txt"
  46. outfile.close()
Also for the 1st program, how can i make it so it keeps asking for a input until 0 is enter. I was only able to make so it kills the program.

Please help me, any help or input would be greatly appreciated. Thanks in advance.
May 24 '08 #1
2 2447
clouddragon
6 New Member
i figured it out most of the program but i am unable to have it write to a outfile.

Expand|Select|Wrap|Line Numbers
  1. L = input("Please enter a number greater than 2: ")
  2. outfile = open('primes-'+str(L)+'.txt','w')
  3. for n in range(2, L): #loop
  4.      for x in range(2, int(n**0.5)+1):  
  5.         if n % x == 0:
  6.             print n #prints the number if it is composite, skips the prime.
  7.             break
Here it prints the solution, but i need it to write to the txt. I know i have to use outfile.write( + '\n') but i was unable to get it to work. Please help. Thanks
May 24 '08 #2
Laharl
849 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. outfile.write(str(n)+'\n')
If you don't call str(), a TypeError is thrown because n is an integer.
May 24 '08 #3

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

Similar topics

5
3496
by: duikboot | last post by:
Hi all, I'm trying to export a view tables from a Oracle database to a Mysql database. I create insert statements (they look alright), but it all goes wrong when I try to execute them in Mysql, because the dates must have quotes on each side. I just don't know how make the dates right. Well I'll just show you the code and some insert statements it generates. Could anyone please help me?
4
1995
by: kevin | last post by:
meta refresh tag is failing , it is supposed to go to the entry1.htm site which is the flash swf file but it doesn't check code please http://members.optusnet.com.au/~kevindauth/ thanks kevin
14
2169
by: TrvlOrm | last post by:
OK. After much playing around, I managed to get my frame page this far.. see code below. BUT...there are still errors with it, and what I would like to have happened is this: 1) On the Left Frame (File LeftEx8_2.html) a series of buttons, which when clicked prompt the user to enter information for background color, text color, link color, title and some text.
23
3286
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
1
3443
by: Chua Wen Ching | last post by:
Hi there, I have some problems when reading XML file. 1. First this, is what i did, cause i can't seem to read "sub elements or tags" values, so i place those values into attributes like this. Before: ----------
4
2280
by: steroche | last post by:
I would REALLY appreciate help please please please! Im sure it is probably blindingly obvious to most of you but I am totally in the dark here!I am lost - i thought i had finally figured out this dataSet updating lark when i realised that i think i am right back at square 1!!! Here's my scenario - i have a SQLDB and i retrieve all my data from that into a dataset and display this to a datagrid(WebForm). I have got this grid sorted and...
28
10278
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when appropriate based on your method names . the GridView has OnRowCommand="GridView1_RowCommand" in the aspx. my problem is that the RowCommand event is firing twice (95% of the time) on the page. the other 5% it only fires once. there's no
2
2596
by: Richard | last post by:
Help please. I am trying to autofill a form text field from a select-box lookup. I have no problem doing this if the select-box is part of the form but because there are 5 possible select boxes for them to choose from and because of the very large number of options in each select box, to save form loading time, I want to put the look-up select boxes on separate (pop-up) pages. (I prefer them not to be hidden divs on the same page) ...
0
2760
by: uno7031 | last post by:
Help Please!!! Adding 5 Days to another Date in an access query Good Morning, Help please…. I am new to access and trying to write a query that will add 5 days between a RecDate and a DLPayDate I created the query in design view of access. Current Query: SELECT PaymentCalculator2.ID, PaymentCalculator2.RecDate, DateAdd("w",5+1,) AS DLPayDate, DateAdd("w",5,) AS DLPayDate2 FROM PaymentCalculator2 WHERE...
0
9474
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
10308
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
10143
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...
0
9939
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...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
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
5375
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...
1
4040
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
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.