Comp 150 – Computer Programming 1

March 13, 2008

 

1.         Lab09a.py – Searching While Loop


#

#  Name:

#  File:  Lab09a.py

#  Date:  March 13, 2008

#

#  Desc: Demonstration of a searching while loop

#

 

def main():

 

    #  display title

   

    print "\nLab09a.py - Seaching a list\n"

 

    #  generate a (random) list of integers using Hailstone sequence

 

    n = input("Enter an integer greater than 1: ")

    print

   

    list = [n]

    while (n != 1):

        if (n % 2 == 1):

            n = 3*n + 1

        else:

            n = n/2

        list.append(n)

 

    #  start ask-before-iterating loop

 

    again = "y"

    while (again == "y"):

 

        #  get value to search for

       

        key = input("Enter a key value to search for: ")

 

        #  search for key in list

       

        i = 0;

        while (i < len(list) and key != list[i]):

            i = i + 1

 

        #  report if value was found or not

       

        if (i < len(list)):

            print "%d was found at position %d" % (key, i)

        else:

            print "%d is not in list" % key

 

        again = raw_input("\nAgain? (y/n): ")

        print

 

main()

 

2.         Lab09b.py – Prime Decomposition Program

 

#

#  Name:

#  File: Lab09b.py

#  Date: March 13, 2008

#

#  Desc: Displays Prime Decomposition

 

def main():

 

    #  display title

 

    print "\nLab09b.py - Prime Decomposition\n"

 

    #  ask-before-iterating loop

 

    again = "y"

    while (again == "y"):

 

        n = input("Enter an integer greater than or equal to 2: ")

        print

 

        print "The prime divisors of", n, "are",

 

        #  search for the divisors of n

       

        k = 2

        while (n != 1):

            if (n % k == 0):    # is k a divisor?

                print k,        # yes - display it and

                n = n/k         #       factor it out

            else:

                k = k + 1       # no - try next integer

        print       

       

        again = raw_input("\nAgain? (y/n): ")

        print

 

main()

 

3.         Lab09c.py – A Prime Counter

 

#

#  Name:

#  File:  Lab09c.py

#  Date:  March 13, 2008

#

#  Desc:  Counts Number of Primes less than or equal to m

 

def main():

 

    #  display title

 

    print "\nLab09c.py - Prime Counter\n"

 

    #  Get integer m

 

    m = input("Enter an integer: ")

 

    count = 0

 

    #  examine for all integers n between 2 and m

 

    for n in range(2, m+1):

 

    #   test if n is prime

   

        k = 2

        while (n % k != 0):

            k = k + 1

     

        if (n == k):            # found a prime - increment count!

            count = count + 1

 

    #  report out

 

    print

    print "The number of primes less than or equal to %d is %d" % (m, count)

 

main()

 

4.         Lab09e.py – The Goldbach Conjecture Program

 

#

#  Name:

#  File:  Lab09e.py : Goldbach Conjecture

#  Date:  March 13, 2008

#

#  Desc: Finds two prines that sum to an even integer. Two

#        different search loops are used

 

def isPrime(n):

    #

    #  returns True if n is prime; otherwise returns False

    #

    k = 2

    while (n % k != 0):

        k = k + 1

    if (n == k):

        return True

    else:

        return False

 

def main():

 

    #  display title

 

    print"\nLab09e.py - Goldbach Conjecture Program\n"

 

    #  ask-before-iterating loop

 

    again = "y"

    while (again == "y"):

 

        #  get an even integer - use filter while loop

 

        while True:

            n = input("Enter an even integer: ")

            if (n % 2 == 0):

                break

            print "Integer entered was odd"

 

        #  search loop #1 to find two primes that sum to n

 

        k = 2       

        while not(isPrime(k) and isPrime(n-k)):

                  k = k + 1

                 

        #   report results

 

        print "\nSearch Loop 1"

        print "%d is the sum of the two primes %d and %d" % (n, k, n - k)

 

        #  search loop #2 to find two primes that sum to n

        #  using a "loop and a half"

       

        k = 2

        while True:

 

            if (isPrime(k) and isPrime(n - k)): # break out condition!

                break

            k = k + 1

 

        #   report results

 

        print "\nSearch loop #2"

        print "%d is the sum of the two primes %d and %d" % (n, k, n - k)

 

        print

        again = raw_input("Again? (y/n): ")

        print

 

main()