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...
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.
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...
Code:
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()
Please help me, any help or input would be greatly appreciated. Thanks in advance.
Comment