#This program builds a list of all prime numbers less than or equal to # a value 'n' entered by the user def main(): print "Hello! This program builds and displays a list of all prime", print " numbers less than or equal to a value entered by the user" while True: # making sure that the user enters a proper value n=input("Please, enter a whole number, greater than 1:") if (n > 1) and type(n) == int: break; print "The number you entered is either less than 2 or a decimal number" primes = [2] # list of primes initially consists of one value counter = 0 # used for counting number of iterations of for and while loops for k in range(3,n+1): d=2 # divisor # checks whether the current k is prime: while k%d > 0 and d**2 < k: d+=1 # increasing the divisor counter +=1 # adding 1 for each iteration of while loop if k%d != 0: #print k, ",",primes primes.append(k) k+=1 #skipping even numbers that are clearly not prime counter +=1 # adding 1 for each iteration of for loop print "the list of all prime numbers less than or equal to",n,"is", print primes print "Complexity (number of iterations of for and while loops:",counter main()