By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

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

P: 6
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
Share this Question
Share on Google+
2 Replies


P: 6
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

Expert 100+
P: 849
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

Post your reply

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