Source: https://ece.uwaterloo.ca/~msachdev/ECE223/Assignment1_Solution_3rd_edition.pdf