Problem Solving in Python 2 by Anne Dawson, PhD

www.annedawson.net




This document shows an example of how one person (AD) solved a problem using the Python 2 programming language.
It is assumed the reader knows how to use the IDLE editor to write simple programs.


                                                                     
                                                                     
                                                                     
                                             
This solution is written in Python 2.
*************************************

Click here for notes on the differences between Python 2 and Python 3.


Click here for the Python 3 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 an input number down to two.

e.g.

IDLE 1.2.1      ==== No Subprocess ====
>>>
Enter any whole number: 20
2
3
5
7
11
13
17
19
>>>



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:


# prime.py
# a program to input any whole number
# and output whether it is a prime number or not.
# WARNING Runs in Python 2

# Programmer: Anne Dawson
# Date: Friday 30th October,7:10 PT



n = input("Enter any whole number: ")
print (n)

prime = True

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, but the output is not always correct.




I am currently (Friday morning at 8:30am) thoroughly testing the program above -

it doesn't work if you input 4

# prime02.py
# a program to input any whole number
# and output whether it is a prime number or not.
# Python version 2.1

# Programmer: Anne Dawson
# Date: Friday 30th October,8:42 PT

repeat = 'y'
prime = True
while repeat == 'y' or repeat == 'Y':
    n = raw_input("Enter any whole number: ")
   
##    print ("The data type of this number is: ")
##    print (type(n))
    n = int(n)
##    print ("The data type of this number is: ")
##    print (type(n))

 
    while i >=2 and i < n:
        if n % i == 0:
            prime = False
            break
        i= i + 1
    if prime == True:
        print ('\n')
        print ( str(n) + " is a prime number" )
    else:
        print ( str(n) + " is a *** not *** prime number" )
   
    repeat = raw_input("\n\nPress y to repeat this program:  ")
    print ('\n\n')
    prime = True




Test Data for prime02.py
------------------------

Input  Output Correct Should be
1      prime     Y
2      prime     Y
3      prime     Y
4      prime     N    NOT prime


#******************************************************************************

# prime03.py
# corrects version prime02.py by adding one line (i = 2)
# This is a program to input any whole number
# and output whether it is a prime number or not.


#  WARNING TO STUDENTS - this program works in Python 2
#  It will not work in Python 3 without changes.
#  See my Python resources for differences between Python 2 and Python 3


# Python version 2.1

# Programmer: Anne Dawson
# Date: Friday 30th October,9:11 PT

repeat = 'y'
prime = True
while repeat == 'y' or repeat == 'Y':
    n = raw_input("Enter any whole number: ")
   
##    print ("The data type of this number is: ")
##    print (type(n))
    n = int(n)
##    print ("The data type of this number is: ")
##    print (type(n))

    i = 2 # version prime02.py did not work because this line wasmissing
    # Python initializes your variables for you on their first use.
    # the first use of i on program prime02.py was the line below
    # where Python gave i a value of 0
    # and therefore the while loop was never entered! thus prime stayed true
   
    while i >=2 and i < n:
        if n % i == 0:
            prime = False
            break
        i= i + 1
    if prime == True:
        print ('\n')
        print ( "prime" )
    else:
        print ( "NOT prime" )
   
    repeat = raw_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 prime04.py, I will add more code to complete the assignment.

after breakfast...


# prime04.py completes Lab 8D (Fall 2009)
# Lab 8D input a whole number
# and output all the prime numers from that number to 1

# prime03.py corrects version prime02.py by adding one line (i = 2)
# This is a program to input any whole number
# and output whether it is a prime number or not.


#  WARNING TO STUDENTS - this program works in Python 2
#  It will not work in Python 3 without changes.
#  See my Python resources for differences between Python 2 and Python 3


# Python version 2.1

# Programmer: Anne Dawson
# Date: Friday 30th October,9:11 PT

repeat = 'y'
prime = True
number = raw_input("Enter any whole number: ")
number = int(number)
n = number
while n > 1:
    i = 2   
    while i >=2 and i < n:
        if n % i == 0:
            prime = False
            break
        i= i + 1
       
    if prime == True:
        print (n)
    n =n - 1
    prime = True


it works but the numbers print out in reverse order to what I require,
so version prime05.py will fix that....




# prime05.py completes Lab 8D (Fall 2009)
# Lab 8D input a whole number
# and output all the prime numers from to 2 to that number

# prime04.py is OK but numbers printed out in reverse order to that required

# prime03.py corrects version prime02.py by adding one line (i = 2)
# This is a program to input any whole number
# and output whether it is a prime number or not.


#  WARNING TO STUDENTS - this program works in Python 2
#  It will not work in Python 3 without changes.
#  See my Python resources for differences between Python 2 and Python 3


# Python version 2.1

# Programmer: Anne Dawson
# Date: Friday 30th October, 12:15 PT


prime = True
n = raw_input("Enter any whole number: ")
n = int(n)
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)
    n =n - 1
    prime = True

# Test runs follow in multiline comments.

# See http://www.annedawson.net/PythonComments.txt
# for important information about multi-line comments

'''

Python 2.5.1 (r251:54863, Jul 31 2008, 23:17:40)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
   
IDLE 1.2.1      ==== No Subprocess ====
>>>
Enter any whole number: 20
2
3
5
7
11
13
17
19
>>>
Enter any whole number: 6
2
3
5
>>>
Enter any whole number: 31
2
3
5
7
11
13
17
19
23
29
>>>


'''




Valid XHTML 1.0!

Valid CSS!

Anne Dawson © 1995-2016 | Anne Dawson All Rights Reserved.