# Notes for Monday and Wednesday, October 9-11

# The Collatz Sequence
#
# Start with any whole number greater than 1.  If the number is even,
# divide it by two, otherwise triple it and add one.  Repeat this
# process until the number becomes 1.  Question: will all numbers
# eventually reach 1?  Most numbers seem to, but no one has ever been
# able to prove that all numbers will.  If you could prove this, you
# would instantly become famous, at least in the world of mathematics!

#---------------------------------------------------------
# this version counts the steps taken by a single number

def collatz1():
    num = input("Number to check: ")
    count = 0
    while num > 1:
        if num % 2 == 0:
            num = num / 2
        else:
            num = 3 * num + 1
        count = count + 1
        print num,
    print
    print "took", count, "steps"

#---------------------------------------------------------
# this version allows the user to repeatedly check
# numbers until a 0 is entered

def collatz2():
    num = input("Number to check (0 to quit): ")
    while num != 0:
        count = 0
        while num > 1:
            if num % 2 == 0:
                num = num / 2
            else:
                num = 3 * num + 1
            count = count + 1
            print num,
        print
        print "took", count, "steps"
        num = input("Next number to check (0 to quit): ")

#---------------------------------------------------------
# same as version 2, but uses a separate function to make
# the code more readable

def collatz3():
    num = input("Number to check (0 to quit): ")
    while num != 0:
        steps = countSteps(num)
        print "%d took %d steps" % (num, steps)
        num = input("Next number to check (0 to quit): ")

def countSteps(n):
    count = 0
    while n > 1:
        if n % 2 == 0:
            n = n / 2
        else:
            n = 3 * n + 1
        count = count + 1
    return count

#---------------------------------------------------------
# this version checks all numbers up to a specified limit

def collatz4():
    limit = input("Check up to: ")
    num = 1
    longest = 1
    longestSteps = 0
    while num <= limit:
        steps = countSteps(num)
        if steps > longestSteps:
            longest = num
            longestSteps = steps
            print "Found a new winner!",
            print "%d took %d steps" % (num, steps)
        num = num + 1
    print "Done!"

#---------------------------------------------------------
# same as version 4 but uses for in place of while

def collatz5():
    limit = input("Check up to: ")
    longest = 1
    longestSteps = 0
    for num in range(1, limit+1):
        steps = countSteps(num)
        if steps > longestSteps:
            longest = num
            longestSteps = steps
            print "Found a new winner!",
            print "%d took %d steps" % (num, steps)
    print "Done!"

