Last modified on 21 November 2011, at 16:28

Python Programming/Loops/Solutions

< Back to Problems

1. Create a program to count by prime numbers. Ask the user to input a number, then print each prime number up to that number.

For this program we will make a list of possibilities up to the user's input, then test each one using Fermat's little theorem, verifying that it is prime, then adding the prime to a list of primes. Finally we print the list of primes.

Comment: All primes fulfill Fermat's little theorem, but some returned results are "pseudoprimes" which are not primes (e.g. 341=11*31, in python: (((2**341)-2)%341 == 0)). More information about finding primes & proving primality

primes = []
                    # Creates a list that we will throw our prime numbers into.  
user = 1 + int(raw_input("What number would you like to count primes up to?  "))
                    # 1 is added on to the user's number so that their number
                    # is counted.  
list = range(2,user)
                    # 0 and 1 are not prime, but our algorithm registers them
                    # as prime, so we start with two
for possibility in list:
    if ((2**possibility)-2)%possibility == 0:
                    # Our algorithm (Fermat's little theorem)  states
                    # that if "i" is an integer and i^p-i divides evenly
                    # into p, p is prime.  We are using 2 in place of i.
                    # See Python Programming/Basic Math for more info.  		
        isprime = True
        test = 2
        while test < possibility and isprime:
                    # Here we verify that the number is in fact prime by
                    # starting at 2 and dividing it by every number lower 
                    # than the potential prime.  See comment above.
            if (possibility%test) == 0:
                    # This ends the while loop causing the next number
                    # to be tested.
                isprime = False
            test = test + 1
        if isprime:
                    # We add each prime to the primes list.  
            primes.append(possibility)
print ("The prime numbers from 0 to", user-1, "are: ")
for possibility in primes:
    print possibility
                    # Print out all the primes!

2. Instruct the user to pick an arbitrary number from 1 to 100 and proceed to guess it correctly within seven tries. After each guess, the user must tell whether their number is higher than, lower than, or equal to your guess.

print "\nIn your head, pick a number between 1 and 100, \ni'll guess it with some hints:\n"
letsstart = raw_input("ready? input anything...\n")
uppers = 100
lowers = 0
guess = 50
boom = 0
for c in range(1,10):
	if boom == 0:
		print "guess: ",guess
		answer = raw_input("Was that\na. too low\nb. too high\nc.  thats right!\n")
		if answer == "c":
			print "I got it!  Thanks for playing.\n"
			boom = 1
		elif answer == "a":
			lowers = guess
			blah = int((uppers-guess)/2)
			guess = guess + blah
		elif answer == "b":
			uppers = guess
			blash = int((guess-lowers)/2)
			guess = guess-blash