exercise2 from Tan

homework from http://www.cs.helsinki.fi/en/courses/582343/2014/s/k/1