#!/usr/bin/python

import math

def hypo(a,b):
    """Returns the length of the hypotenuse of a right triangle with the given legs"""
    # algorithm based on the Pythagorean theorem
    return math.sqrt(a*a+b*b)

def fibonacci(n):
    """Returns the nth Fibonacci number, for `n' a non-negative integer"""
    if type(n) != type(1) or n<0:
        raise Exception('bad argument to fibonacci')
    if n<2:
        return n
    else:
        # what a horrible algorithm!  Never do this!!
        return fibonacci(n-1)+fibonacci(n-2)

def gcd(a,b):
    """Returns the greatest common divisor of the two arguments.
Example: gcd(9,8)=1, since 9 and 8 are relatively prime, but
gcd(24,30)=6, since 6 divides both 24 and 30."""
    # This implementation is Dijkstra's method
    print("a is %d and b is %d" % (a,b))
    if a == b:
        return a
    elif a > b:
        return gcd(a-b,b)
    else:
        return gcd(a,b-a)

def triangular(max):
    """Generates a triangular list of lists up to the given max"""
    result = []
    # range() gives you a list of integers; "for" iterates over lists
    for n in range(max):
        result.append(list(range(n)))
    for elt in result:
        print(elt)

if __name__ == '__main__':
    if hypo(3,4) != 5:
        print('error in hypo:  hypo(3,4) returns ',hypo(3,4))
    print('The first ten Fibonacci numbers are')
    for i in range(10):
        print(fibonacci(i),' ', end=' ')
    print()
    print('testing gcd(20,45)')
    if gcd(20,45) != 5:
        print('error in gcd:  gcd(20,45) returns ',gcd(20,45))
    print("here's a list of 5 lists")
    triangular(5)
    
