`Friday 3rd June 2011, 17:42 PT, AD`

```
``````
```**This document shows an example of how one person (AD) solved a problem using the Python 3 programming language.
It is assumed the reader knows how to use the IDLE editor to write simple programs.**
This solution is written in Python 3.
*************************************
Click here for notes on the differences between Python 2 and Python 3.
Click here for the Python 2 Solution.
This page shows a series of algorithms and programs which will eventually end up solving the problem -
to print out all the prime numbers in the range of two to an input number.
e.g.
Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
Enter any whole number: 20
2
3
5
7
11
13
17
19
Press y to repeat this program: n
>>>
Firstly, to solve this problem I have to understand what a prime number is.
If I don't understand what a prime number is, then I have zero chance of solving the problem.
So, what is a prime number?
A prime number is a number which when divided (integer division) by the range of numbers from itself down to 1
only has a remainder of zero for a division by itself and one.
For example:
13 % 13 is 0
13 % 12 is 1
13 % 11 is 2
13 % 10 is 3
13 % 9 is 4
13 % 8 is 5
13 % 7 is 6
13 % 6 is 1
13 % 5 is 3
13 % 4 is 1
13 % 3 is 1
13 % 2 is 1
13 % 1 is 0
13 is a prime number
Is 6 a prime number?
6 % 6 is 0
6 % 5 is 1
6 % 4 is 2
6 % 3 is 0
6 % 2 is 0
6 % 1 is 0
6 is not a prime number because the numbers 3 and 2 divide into 6 with no remainder.
So, an explanation is shown above, so how do I solve the problem by computer program.
The explanation is clear as shown above.
I have to input the number to be checked, let's call that n.
For all numbers 2 through n - 1, I have to check the remainder after an integer division .
If any result is 0, the number is NOT a prime number.
The Algorithm:
-------------
Repeat for all numbers 2 through n - 1
check the remainder after an integer division of n
if the result is 0
set prime to be false
The Python Code:
---------------
while i >=2 and i < n:
if n % i == 0
prime = False
break
i= i + 1
Important notes on indentation (spacing) in a Python program.
A program to input a whole number
and output if it is a prime number or not:
#################################################################
# prime01_P3.py
# a program to input any whole number
# and output whether it is a prime number or not.
# WARNING Runs in Python 3
# Programmer: Anne Dawson
# Date: Monday 2nd November 2009, 8:51 PT, AD
n = int(input("Enter any whole number: "))
print (n)
prime = True
i = 2
while i >=2 and i < n:
if n % i == 0:
prime = False
break
i= i + 1
if prime == True:
print ( str(n) + " is a prime number" )
else:
print ( str(n) + " is a not prime number" )
#################################################################
The program above runs correctly.
#################################################################
# prime02_P3.py
# a program to input any whole number
# and output whether it is a prime number or not.
# WARNING Runs in Python 3
# Programmer: Anne Dawson
# Date: Monday 2nd November 2009, 8:51 PT, AD
repeat = 'y'
prime = True
while repeat == 'y' or repeat == 'Y':
n = int(input("Enter any whole number: "))
i = 2
while i >=2 and i < n:
if n % i == 0:
prime = False
break
i= i + 1
if prime == True:
print ( str(n) + " is a prime number" )
else:
print ( str(n) + " is a not prime number" )
repeat = input("\n\nPress y to repeat this program: ")
print ('\n\n')
prime = True
#################################################################
TESTED OK - for any whole number, correctly says if prime or not
Now in version prime03_P3.py, I will add more code to complete the assignment.
#################################################################
# prime03_P3.py
# a program to input any whole number
# and output whether it is a prime number or not.
# WARNING Runs in Python 3
# Programmer: Anne Dawson
# Date: Tuesday 3rd November 2009, 6:01 PT, AD
repeat = 'y'
prime = True
while repeat == 'y' or repeat == 'Y':
n = int(input("Enter any whole number: "))
for n in range (2,n):
i = 2
while i >=2 and i < n:
if n % i == 0:
prime = False
break
i= i + 1
if prime == True:
print ( n )
prime = True
repeat = input("\n\nPress y to repeat this program: ")
print ('\n\n')
#################################################################
TESTED works ok
#################################################################
#################################################################
# #
# Test runs follow in multiline comments. #
# #
# See http://www.annedawson.net/PythonComments.txt #
# for important information about multi-line comments #
# #
#################################################################
'''
Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Enter any whole number: 20
2
3
5
7
11
13
17
19
Press y to repeat this program: y
Enter any whole number: 6
2
3
5
Press y to repeat this program: y
Enter any whole number: 31
2
3
5
7
11
13
17
19
23
29
Press y to repeat this program: n
'''