Ad hoc, copy from ProjectEuler Problem 54
Ad hoc