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.


    
    
    

    (ex_5_7)


    
    
    

    (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.


    
    
    

    (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.


    
    
    

    (ex_5_13)


    
    
    

    (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)).


    
    
    

    (ex_5_14)

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


    
    
    

    (ex_5_15)


    
    
    

    (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.




(scratch_02)

Next Section - Boolean Values and Boolean Expressions