How do you find the first N prime numbers in python?

Question:

I am pretty new to python, so I don’t fully understand how to use loops. I am currently working on a piece of code that I have to find the first N prime numbers.
The result that is desired is if you input 5, it outputs 2, 3, 5, 7, and 11, but no matter what I input for ‘max’, the output always ends up being 2 and 3. Is there a way to improve this?

max=int(input("How many prime numbers do you want: "))
min=2
while(min<=(max)):
  for c in range(2, min):
    if min%c==0:
      break
  else:
    print min
    min=min+1
Asked By: DSX

||

Answers:

You only increment min in the else block, i.e., if min % c is nonzero for all c, i.e., if min is prime. This means that the code won’t be able to move past any composite numbers. You can fix this by unindenting min=min+1 one level so that it lines up with the for and else.

Answered By: jwodder
number = int(input("Prime numbers between 2 and "))
for num in range(2,number + 1):
       if num > 1:
            for i in range(2,num):
                if (num % i) == 0:
                     break
            else:
                print(num)
Answered By: van neilsen

Solution: Get the nth prime number entry. Iterate through each natural numbers for prime number and append the prime number to a list. Terminate the program when length of a list satisfies the user nth prime number entry.

# Get the number of prime numbers entry.
try:
    enterNumber = int(input("List of nth prime numbers: "))
except:
    print("The entry MUST be an integer.")
    exit()
startNumber = 1
primeList = []
while True:
    # Check for the entry to greater than zero.
    if enterNumber <= 0:
        print("The entry MUST be greater than zero.")
        break
    # Check each number from 1 for prime unless prime number entry is satisfied.
    if startNumber > 1:
        for i in range(2,startNumber):
            if (startNumber % i) == 0:
                break
        else:
            primeList.append(startNumber)
    if (len(primeList) == enterNumber):
        print(primeList)
        break
    else:
        startNumber = startNumber + 1
        continue
Answered By: Pramit

The following code will give you prime numbers between 3 to N, where N is the input from user:

number = int(input("Prime numbers between 2, 3 and ")) 
for i in range(2,number):
    for j in range(2,int(i/2)+1):
        if i%j==0:
            break
        elif j==int(i/2):
            print(i)

You can see to check a number i to be prime you only have to check its divisibility with numbers till n/2.

Answered By: slashstar

Try that :

n = int(input("First N prime number, N ? "))
p = [2]
c = 2

while len(p) < n:
    j = 0
    c += 1
    while j < len(p):
        if c % p[j] == 0:
            break
        elif j == len(p) - 1:
            p.append(c)
        j += 1
print(p)
Answered By: Alix R.

Its simple. Check the below code, am sure it works!

N = int(input('Enter the number: ')
i=1
count=0
while(count<N):
      for x in range(i,i+1):
          c=0
          for y in range(1,x+1):
              if(x%y==0):
                 c=c+1
          if(c==2):
             print(x)
             count=count+1
      i=i+1
Answered By: kushal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.