Source: http://people.scs.carleton.ca/~mjhinek/W13/COMP2401/notes_and_code/mid/2401-mid-sol.pdf