1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python code for generating prime# list and evaluting a number

  1. Nov 8, 2011 #1
    1. The problem statement, all variables and given/known data
    I am attempting to allow a user to enter a number and check to see if it is in a list of already generated prime numbers


    Here's my attempt

    Where can I put the code to allow the number to be checked, the prime list generator works fine alone


    Code (Text):
    def buildPrimeList ():
       
        primeList = [1, 2]
        possiblePrime = 3
        print "To calculate a list of prime values less than or equal a number..."

        x=int(raw_input("Enter a number higher then 3   "))
       
        while (possiblePrime <= x):  # changed input method
            divisor = 2
            isPrime = True
            y= int(raw_input("To see if a number is in the list enter that number enter -1 to exit: "))      
            while (divisor < possiblePrime ): #change condition to only less than possible prime
                if ((possiblePrime % divisor) == 0):
                    isPrime = False
              divisor = divisor + 1
           
            if (isPrime):
                primeList.append(possiblePrime)
                print primeList
               
            if y== -1:
                print "Goodbye"
                break
            if y in primeList:
                print y, "Is in the list"
            if y not in primeList:
                print y, "Is not in the list"    
                     
          possiblePrime = possiblePrime + 2
           
               
        return primeList
       
       
    buildPrimeList()

           
     
    Last edited: Nov 8, 2011
  2. jcsd
  3. Nov 8, 2011 #2

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    Why are you asking them to enter a number to check if it's in the list before you are done building the list? Why don't you move the y stuff outside of the loop that builds the list?
     
  4. Nov 9, 2011 #3
    The user is supposed to be repeatedly prompted, that is why the y=raw_input is in the loop
     
  5. Nov 9, 2011 #4

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    It's still in the wrong place. Make a loop outside of buildPrimeList and put your input there.
     
  6. Nov 9, 2011 #5
    Thanks,

    I did this and it works well enough, any general suggestions on the code

    Code (Text):
    def buildPrimeList ():
       
        primeList = [1, 2]
        possiblePrime = 3
        print "To calculate a list of prime values less than or equal a number..."
       
        x=int(raw_input("Enter a number higher then 3   "))
       
        while  (possiblePrime <= x):  # for personal preference changed range to raw_input, so that the range can be arbitrary, though at a certain point a large range will consume a-lot of memory
            divisor = 2
            isPrime = True
                 
            while (divisor < possiblePrime ):
                if ((possiblePrime % divisor) == 0):
                    isPrime = False
                divisor = divisor + 1
            if (isPrime):
                primeList.append(possiblePrime)
           
            possiblePrime = possiblePrime + 2
        while (primeList==primeList):  
            y= int(raw_input("To see if a number is in the list enter that number enter -1 to exit: "))    
            if y== -1:
                print "Goodbye"
                break
            if y in primeList:
                print y, "Is in the list"
            if y not in primeList:
                print y, "Is not in the list"
               
        return primeList  
             
       
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Python code for generating prime# list and evaluting a number
Loading...