A prime number is any positive integer (greater than one) that has only two factors, one and itself. You can create a simple Python program, PrimeGen.py, to test whether an integer is prime, and simultaneously display all prime numbers that are less than or equal to that integer.

**Program Overview –** As shown in the first image above, PrimeGen.py executes as follows:

1 – PrimeGen.py prompts you to enter an integer, n.

2 – Sets the ‘for’ loop range to test all integers in the range 2 to n+1, and set the divisor x to 2.

3 – The ‘while’ loop uses the modulo operator (%) to divide the current n by x to test for not prime (prime=False), increments x. and repeats until it finds a prime (prime=True) and returns to the ‘for’ loop.

4 – The ‘for’ loop appends the prime number to array L and then repeats to test the next n.

5 – The ‘for’ loop and its ‘while’ loop repeat steps 2 through 4 for each remaining integer in the range 2 to n+1.

6 – After the ‘for’ loop tests n+1, PrimeGen.py displays its generated primes in array L, and then exits.

For example, as shown in the second image above, if you enter 139, PrimeGen.py displays all prime numbers between 2 and 139.

**Creating your Prime Number Generator –** To create your PrimeGen.py Python Program, do the following:

1 – Install both Python 3 and IDLE for Python 3.

2 – Start IDLE, click “File” and then click “New Window.”

3 – Click “File,” click “Save As,” type “PrimeGen” into the File-name field, select a convenient location (such as Desktop) and then click “Save.”

4 – Do either of the following:

* – Copy PrimeGen and paste it into your PrimeGen.py window. For more information, see Running Python Examples.

* – Click on the first image above to expand it, copy it to a word-processor page, print that page and then type the code into your PrimeGen.py window.

5 – Run your PrimeGen.py program according to the section below.

**Running your Prime Number Generator –** You can run PrimeGen.py to test whether an integer is prime, and simultaneously display all prime numbers less than or equal to that integer. To run your PrimeGen.py Python Program, do the following:

1 – Create PrimeGen.py according to the section above.

2 – Start IDLE, click “File,” click “Open,” navigate to and click “PrimeGen.py.”

3 – Click “Run,” click “Run Module” and then enter an integer to display, in the Python Shell, all prime numbers less than or equal to that integer.

**Note:** Be patient if you enter a large integer; it requires more processing time than a small integer.

Consider the following examples:

* – As shown in the third image above, if you enter 613, the Python Shell displays all primes from 2 to 613 because 613 is prime.

* – As shown in the fourth image above, if you enter 615, the Python Shell displays all primes from 2 to 613 because 615 is not prime.

* – As shown in the fifth image above, if you enter 617, the Python Shell displays all primes from 2 to 617 because 617 is prime.

**Sources:**

* – Professor Chris Caldwell, “The Prime Pages”, The University of Tennessee at Martin

* – Brian Heinold, “Inroduction to Programming Using Python”, Mount St. Mary’s University