Here's the automated way of Jack's paper and pencil method.