# Programmer: Sriram Pemmaraju # Date: Jan 30th, 2012 # This program reads a positive integer, greater than 1 and # determines whether this integer is a prime or not. # It contains timing statements that tell us the amount of time # the program takes to run. import time # the time module is needed for functions that help time our code import math # the math module is needed for the sqrt function # Input n = int(raw_input("Please type a positive integer, greater than 1: ")) factor = 2 # initial value of possible factor isPrime = True # variable to remember if n is a prime or not factorUpperBound = math.sqrt(n) # because we only need to generate candidate factors until sqrt(n) start = time.time() # remembers the time at which the loop started # loop to generate and test all possible factors while factor <= factorUpperBound: # test if n is evenly divisible by factor if (n % factor == 0): isPrime = False break factor = factor + 1 end = time.time() # remembers the time at which the loop ended elapsedTime = end - start # Output if isPrime: print n, " is a prime." else: print n, " is a composite." print "The while-loop took ", elapsedTime, "seconds."