Posts

Showing posts with the label primes

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

How do you find the first N prime numbers in python? 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 You don't really want 10 prime numbers then, just the prime numbers between 2 and 10. – pkpkpk Jun 30 at 18:27 This is a side comment. If you are new to Python I would strongly recommend jumping on 3.x Python (3.6 is stab...