Hi All,

I am new to the programming world. I was just writing this code to generate N prime numbers. User should input the value for N which is the total number of prime numbers to print out. I have written this code but it doesnt throw the desired output. Instead it prints the prime numbers till the Nth number.
For eg.: User enters the value of N = 7.
Desired output: 2, 3, 5, 7, 11, 13, 19
Actual output: 2, 3, 5, 7

Kindly advise.

i=1
x = int(input("Enter the number:"))

for k in range (1, (x+1), 1):
c=0;
for j in range (1, (i+1), 1):
a = i%j
if (a==0):
c = c+1

if (c==2):
print (i)
else:
k = k-1

i=i+1
print (i)
else:
k = k-1

i=i+1

Welcome to the forums! For the future, please use the code tags around your script to make it easier to read :) (http://www.daniweb.com/forums/announcement114-3.html)

This does look like a homework question, so please read the rules about posting homework questions here: http://www.daniweb.com/forums/announcement114-2.html

Here is an example for comming up with prime numbers (up to 1230):

I commented to show you the logic I used, and how it's done..

>>> def getprimes(x):
	primes = []

	# Loop through 9999 possible prime numbers
	for a in range(1, 10000):

		# Loop through every number it could divide by
		for b in range(2, a):

			# Does b divide evenly into a ?
			if a % b == 0:
				break

		# Loop exited without breaking ? (It is prime)
		else:
			# Add the prime number to our list
			primes.append(a)

		# We have enough to stop ?
		if len(primes) == x:
			return primes
		
>>> getprimes(5)
[1, 2, 3, 5, 7]
>>> getprimes(7)
[1, 2, 3, 5, 7, 11, 13]

Another cool way of doing this (and because i like complicated lists):

>>> [p for p in [a for a in range(1,1000) if all([(a % b != 0) for b in range(2, a)])][:input('How many prime numbers? ')]]
How many prime numbers? 10
[1, 2, 3, 5, 7, 11, 13, 17, 19, 23]
>>>

That way is slower because it will allways create a full list of prime numbers (169 of them) and then just give you the ones you ask for... But its fun to work out loops like that :)

Edited 7 Years Ago by lukerobi: n/a

Thank you for sharing your logic. I was just wondering whats going wrong in my logic. So if you could help me in correcting my logic will be really helpful for learners like me.

i=1
x = int(input("Enter the number:"))

for k in range (1, (x+1), 1):
    c=0;
    for j in range (1, (i+1), 1):
        a = i%j
        if (a==0):
            c = c+1

    if (c==2):
        print (i)
    else:
        k = k-1

    i=i+1

If you put the number of primes you want, you called it x, in the outer loop range() you will get x numbers, but since not all of them are prime it will print less than x numbers.

You need to make the outer loop an endless loop with a counter and an exit condition to break out of the outer loop ...

i=1
x = int(input("Enter the number:"))

counter = 0
while True:
    c=0;
    for j in range (1, (i+1), 1):
        a = i%j
        if (a==0):
            c = c+1

    if (c==2):
        print (i)
        counter = counter + 1
        if counter >= x:
            break

    i=i+1

BTW, your coding style could be improved, but it's readable enough.

A note to lukerobi, 1 is not a prime number.

Edited 7 Years Ago by vegaseat: 1

Comments
what is c for?
This question has already been answered. Start a new discussion instead.