Exercises

  1. Write a fruitful function sumTo(n) that returns the sum of all integer numbers up to and including n. So sumTo(10) would be 1+2+3...+10 which would return the value 55. Use the equation (n * (n + 1)) / 2.


     
    1
    def sumTo(n):
    2
        # your code here
    3
    
    
    

    (ex_5_7)


    14
     
    1
    from test import testEqual
    2
    3
    def sumTo(n):
    4
        result = (n * (n + 1)) / 2
    5
        return result
    6
    7
    # Now lets see how well this works
    8
    t = sumTo(0)
    9
    print("The sum from 1 to 0 is",t)
    10
    t = sumTo(10)
    11
    print("The sum from 1 to 10 is",t)
    12
    t = sumTo(5)
    13
    print("The sum from 1 to 5 is",t)
    14
    
    
    

    (q7_answer)

  2. Write a function areaOfCircle(r) which returns the area of a circle of radius r. Make sure you use the math module in your solution.


    12
     
    1
    from test import testEqual
    2
    3
    def areaOfCircle(r):
    4
        # your code here
    5
    6
    t = areaOfCircle(0)
    7
    testEqual(t,0)
    8
    t = areaOfCircle(1)
    9
    testEqual(t,math.pi)
    10
    t = areaOfCircle(100)
    11
    testEqual(t,31415.926535897932)
    12
    
    
    

    (ex_5_8)

  3. Rewrite the function sumTo(n) that returns the sum of all integer numbers up to and including n. This time use the accumulator pattern.


    3
     
    1
    def sumTo(n):
    2
        # your code here
    3
    
    
    

    (ex_5_13)


    14
     
    1
    def sumTo(n):
    2
        sum = 0
    3
        for i in range(1,n+1):
    4
            sum = sum + i
    5
        return sum
    6
    7
    # Now lets see how well this works
    8
    t = sumTo(0)
    9
    print("The sum from 1 to 0 is",t)
    10
    t = sumTo(10)
    11
    print("The sum from 1 to 10 is",t)
    12
    t = sumTo(5)
    13
    print("The sum from 1 to 5 is",t)
    14
    
    
    

    (q13_answer)

  4. Write a function called mySqrt that will approximate the square root of a number, call it n, by using Newton’s algorithm. Newton’s approach is an iterative guessing algorithm where the initial guess is n/2 and each subsequent guess is computed using the formula: newguess = (1/2) * (oldguess + (n/oldguess)).


    3
     
    1
    2
    3
    
    
    

    (ex_5_14)

  5. Write a function called myPi that will return an approximation of PI (3.14159...). Use the Leibniz approximation.


    3
     
    1
    2
    3
    
    
    

    (ex_5_15)


    17
     
    1
    def myPi(iters):
    2
        ''' Calculate an approximation of PI using the Leibniz
    3
        approximation with iters number of iterations '''
    4
        pi = 0
    5
        sign = 1
    6
        denominator = 1
    7
        for i in range(iters):
    8
            pi = pi + (sign/denominator)
    9
            sign = sign * -1  # alternate positive and negative
    10
            denominator = denominator + 2
    11
    12
        pi = pi * 4.0
    13
        return pi
    14
    15
    pi_approx = myPi(10000)
    16
    print(pi_approx)
    17
    
    
    

    (q15_answer)

  6. Write a function that takes in input 10 numbers and returns the minimum, the sum, the maximum, and the average.

Note

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.


3
 
1
2
3


(scratch_02)

Next Section - Boolean Values and Boolean Expressions