CMIMC 2017 Power Problem 9

Fix the algorithm, and prove correctness.