Homework for Week 3 - First Python programs

Homework is due on Monday, 22/05/2017, 2pm

  • You only learn a programming language by actively praticing and using it! I therefore strongly advise you to work on the homework problems.
  • Please discuss the problems with your student peers in case of problems.
  • Please send your solutions via mail to Joseph.
    • He will correct them and give you feedback on your solutions. I also will talk about common problems in later classes.
    • You can hand in Jupyter notebooks or Python scripts.
    • If you hand in scripts, please use the proposed names to make live easier for Joseph!
    • You are free to hand in group solutions - there is no limit on the number of students who want to work in a group! Please put the names of the students involved in your groups clearly within the Jupyter notebooks or as comments within scripts.
    • Your code needs to be well and appropriately commented!

Approximation of $\pi$ with the Wallis product

$\pi$ can be approximated with the infinite product $$\pi = 2\prod_{i=1}^{\infty}\frac{4i^2}{4i^2-1}$$ Write a Python program which approximates $\pi$ by the finite products $$\pi_{\rm approx} = 2\prod_{i=1}^{n}\frac{4i^2}{4i^2-1}$$ with $n\in \{50, 100, 1000, 10000, 20000\}$ and compare the results with the value $\pi\approx 3.1415926$ by reporting absolute $\epsilon_{\rm abs} = |\pi - \pi_{\rm approx}|$ and relative $\epsilon_{\rm rel} = \frac{|\pi - \pi_{\rm approx}|}{\pi}$ errors of the approximations.

If you hand in a script for this task, please name it wallis.py


In [ ]:
# Your solution here

Fibonacci Numbers

The infinite series of Fibonacci numbers $F_n$ is defined as $F_1=1$, $F_2=1$ and $F_n = F_{n-1} + F_{n-2}$. Write a python program which calculates the sum of the first 1000 Fibonacci numbers!

If you hand in a script for this problem, please name it fibonacci.py


In [ ]:
# your solution here

Chicken McNuggets

Mc Donalds sells its Chicken McNuggets in packages of 6, 9 and 20 pieces. In this exercise we want to investigate, which number of nuggets can be bought with an arbitrary combination of these package sizes.

  • Write a python-function can_buy which takes two arguments:

    • The (integer) number $N$ for the number of nuggets we would like to buy. This is a required argument.
    • An optional boolean-argument verbose whose default is set to True. See below for an explanation of the purpose of this argument.

    The function should return True if $N$ nuggets can be bought in package combinations of 6, 9 and 20 and False otherwise.

    If the verbose-argument is set to True, the function should either print:

    • "Sorry, the nuggets cannot be bought" if it is not possible to buy $N$ nuggets.
    • All possible combinations of package sizes in which the nuggets can be bought if that is possible. As an advanced test case, you should find the following five possibilities for $N=60$ nuggets: (p_20, p_9, p_6) = (3, 0,0); (0, 6, 1); (0, 4, 4); (0, 2, 7); (0, 0, 10).

    If the verbose-argument is set to False, the function should not print anything,

    (Hint: A computer is ideal to determine a solution to a problem by trying out and testing all possibilities. You can solve this problem with this strategy and three nested while-loops)

    If you hand in a script for this exercise, please name it nuggets.py. You can put code and comments for the following parts of this exercise into this script.

  • Use your program to show that you can buy $N=50, 51, \dots, 55$ nuggets.
  • Reason that you can buy any amount of nuggets $N>M$ if you can buy $M-5, M-4, \dots, M$ nuggets.
  • Determine the maximum number of nuggets that cannot be bought in packages of 6, 9 and 20 pieces.

In [ ]:
# your solution here