Computing exponents and sums

Let's think back to what $a^b$ means. We can write it as a product: $\Pi_{i=0}^{b-1} a$. The product notation is directly translatable into a for-loop:

In [1]:
def my_pow(a, b):
    '''Return a^b
    '''
    res = 1
    for i in range(b):
        res = res * a

    return res

Let's now write a function that computes the product $a\times b$ using a for-loop (without using *). We can write the product using sigma notation as follows: $\Sigma_{i=0}^b a = \Sigma_{i=0}^a b$

In [2]:
def my_prod(a, b):
    '''Return a*b
    '''
    res = 0 #why is this 0 and not 1 this time?
    for i in range(b):
        res = res + a
    
    return res

For Problem 1, the summation is more involved -- each term depends on i (or n, depending on what variable you decide to use), but the principle is the same.