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... - n = input ("Please enter a number greater than 1 (0 for exit):")
-
if n == 0:
-
print exit()
-
def get_factors(n):
-
List1 = []
-
List2 = []
-
List3 = []
-
for x in range(2, int(n**0.5)+1):
-
if n % x == 0:
-
List1 = List1 + [x]
-
for y in List1:
-
c = n / y
-
if c not in List2:
-
List2 = List2 + [c]
-
List3 = List1 + List2
-
List3.sort()
-
return List3
-
-
#see if number is a prime
-
def prime(n):
-
-
#range starts with 2 and only needs to go up the squareroot of n
-
for x in range(2, int(n**0.5)+1):
-
if n % x == 0:
-
return False
-
return True
-
-
-
#print whther its a prime or not
-
if prime(n):
-
print n, "is a not a composite number"
-
-
else:
-
print n, "is a composite number!"
-
print get_factors(n)
-
-
-
#program 2
-
n = input("Please enter a number greater than 2: ")
-
outfile = open('primes-'+str(n)+'.txt','w')
-
-
-
outfile.write("prime(n)" + '\n')
-
-
print "Found", len(prime_number(n)), "numbers; please check file primes-",n,".txt"
-
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.
2 2447
i figured it out most of the program but i am unable to have it write to a outfile. - L = input("Please enter a number greater than 2: ")
-
outfile = open('primes-'+str(L)+'.txt','w')
-
for n in range(2, L): #loop
-
for x in range(2, int(n**0.5)+1):
-
if n % x == 0:
-
print n #prints the number if it is composite, skips the prime.
-
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
Laharl 849
Recognized Expert Contributor - outfile.write(str(n)+'\n')
If you don't call str(), a TypeError is thrown because n is an integer.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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
|
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.
|
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
|
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:
----------
| |
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...
|
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
|
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)
...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |