Project Euler 385(1)

http://projecteuler.net/index.php?section=problems&id=385

この考え方でできるような気がするんだけど、なぜかうまくいかない。