Source: http://www.maths.ed.ac.uk/~chris/ALG/w4_sol.pdf