Reimplemented PHP solution to problem 1 in constant time.