Python code for generating prime# list and evaluting a number

In summary, the conversation is about a code that allows a user to enter a number and checks if it is in a list of prime numbers. The code also prompts the user to enter a number to check if it is in the list before the list is fully built. The expert suggests moving the prompt outside of the loop that builds the list for better efficiency. The expert also suggests using raw_input for a larger range and provides a modified version of the code with the prompt outside the loop.
  • #1
mr.me
49
0

Homework Statement


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:
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:
Technology news on Phys.org
  • #2
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?
 
  • #3
The user is supposed to be repeatedly prompted, that is why the y=raw_input is in the loop
 
  • #4
It's still in the wrong place. Make a loop outside of buildPrimeList and put your input there.
 
  • #5
Thanks,

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

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

1. How do I generate a list of prime numbers in Python?

To generate a list of prime numbers in Python, you can use a for loop to iterate through a range of numbers and check if each number is prime using the modulus operator to see if it is divisible by any number other than itself and 1. If the number is prime, add it to a list. Once the loop is finished, the list will contain all prime numbers within the specified range.

2. Can I generate a list of prime numbers up to a specific number?

Yes, you can generate a list of prime numbers up to a specific number by using the same method as mentioned in the previous question but with a slight modification. Instead of iterating through a range of numbers, you can use a while loop and increment the number until it reaches the specified limit. This way, the list will only contain prime numbers up to the specified number.

3. How can I evaluate if a number is prime in Python?

To evaluate if a number is prime in Python, you can use the same method as mentioned in the first question. Check if the number is divisible by any number other than itself and 1 using the modulus operator. If the result is 0, then the number is not prime. Otherwise, it is a prime number.

4. Is there a more efficient way to generate a list of prime numbers in Python?

Yes, there are more efficient ways to generate a list of prime numbers in Python. One method is to use the Sieve of Eratosthenes algorithm, which involves creating a list of numbers and crossing out all multiples of each prime number until only prime numbers are left. This method is more efficient for generating a large list of prime numbers.

5. Can I use a library or module for generating a list of prime numbers in Python?

Yes, there are several libraries and modules available in Python that can generate a list of prime numbers. Some popular ones include the SymPy library, the gmpy2 module, and the primesieve module. These libraries and modules offer more efficient and optimized methods for generating prime numbers in Python.

Similar threads

  • Programming and Computer Science
Replies
22
Views
761
Replies
5
Views
886
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
7
Views
428
  • Programming and Computer Science
Replies
1
Views
776
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
3
Views
316
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
3
Views
996
Back
Top