Back to Math Projects

Python Prime Test Project

Instructions:

Use the code below to start the prime test project. This is the starter code for the first lessons in our week one covering python.
This code should be copied and pasted into your primetest.py file.

#-------------------------------------------------------------------------------
# Name:        David @ ElectricTeaching.com
# Purpose:
#
# Author:      Administrator
#
# Created:     11/12/2011
# Copyright:   © Administrator 2011
# License:     Electric Teaching © 2011
#-------------------------------------------------------------------------------

# using the math library (and 2.7 python)

import math

# This is the main (and only) function.  
def primetest():
    x=raw_input('Number to test ')
    x=int(x)                    # raw input is a 'string' of symbols,  we could use input(' ')
                                #  we tell the computer to change n to an integer
    n=math.sqrt(x)              # we only need to check up to sqaure root of the value
    n=int(n)

    # Run through n iterations in a repeat loop to find any factors
        #  A Percent symbol is a remainder function after dividing the value after.
        #  An alternavtive method is to truncate after the dividing and testing
        #    to see if equal to the original quotient.
    x=2.0
    for i in range(n):
        div = float(n/x)
        divtest = math.trunc(div)
        if div == divtest:
            print n,(' is a composite number with a factor of '),x
            return
        x=x+1
    print x, ('is prime')

# The method python structure uses to start a program.
# Every function (or set of instructions) are read and stored in memory
#   then the program begins to execute the functions with this if statement.

if __name__=='__main__':
    primetest()
                    

Instructions:

This projects asks students to use the primetest function (above) and practice passing variables from function to function with the objective of finding all prime numbers up to a value. The code below is one method (not necessarily the best code as I am trying to teach different loops and styles of handling variables. Challenge: change the code to ask the user for staring value and ending value to find primes in a range. Then use the code to explore if there are more primes between 1000 and 2000 or between 2000 and 3000.

#-------------------------------------------------------------------------------
# Name:        David @ ElectricTeaching.com
# Purpose:
#
# Author:      Administrator
#
# Created:     11/12/2011
# Copyright:   © Administrator 2011
# License:     Electric Teaching © 2011
#-------------------------------------------------------------------------------

# David's Prime test to find First primes
# test a number for factors by dividing in a loop to see if prime

import math

def primetest(n):
    n=int(n)
    s=int(math.sqrt(n))
    x=2.0				# if x = 2 without the decimal '.0' then x will be only an integer.  
    					# We need it to be a 'real' value that will give decimal answers in calculations later in the code
    for i in range(s):
        div = n/x			# an integer divided by a 'real' value will be 'real' (with decimals)
        divtest = math.trunc(div)	#  math.trunc deletes the decimal part (not rounds).  The 'math.' part means from the math library.
                                        # You could use a '%' to determine fraction part (and test to see if equal to 0)to improve this code. 
        if div == divtest:
            test='false'
            return test
        else:
            x=x+1
    test = 'true'
    return test

# finding the first primes up to a value
def firstprimes(total):
    testnum=2
    primecount=0							# keep track of number of primes
    while testnum<total+1:
        if primetest(testnum)=='true':
            print testnum,' is prime.'
            primecount=primecount+1
            testnum = testnum+1
        else:
            testnum = testnum+1
    print primecount						# display total number of primes
# this is the way python runs a program
if __name__=='__main__':
    n = raw_input('Enter a number to find the first primes up to this number: ')
    n=int(n)
    firstprimes(n)
                    
David Johns and Electric Teaching ~ All Rights Reserved © 2013