Project Euler 316(4)

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


いちおうできたけど、遅い。
うまくメモ化できない。