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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clouddragon
    New Member
    • Apr 2008
    • 6

    #1

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

    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...

    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()
    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.
  • clouddragon
    New Member
    • Apr 2008
    • 6

    #2
    i figured it out most of the program but i am unable to have it write to a outfile.

    Code:
    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

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      [code=python]outfile.write(s tr(n)+'\n')[/CODE]
      If you don't call str(), a TypeError is thrown because n is an integer.

      Comment

      Working...