modified my algorithm for problem 455, using ideas from the forums