Skip to content

Instantly share code, notes, and snippets.

@tamurashingo
Created February 21, 2013 14:01
Show Gist options
  • Save tamurashingo/5004906 to your computer and use it in GitHub Desktop.
Save tamurashingo/5004906 to your computer and use it in GitHub Desktop.
Project Euler 4
--
-- 3桁x3桁で6桁の回文積をつくる
--
-- 3桁x3桁の集合
[ x*y | x <- [100..999], y <- [100..999] ]
-- XxYとYxXは同じなので、片方だけにする
[ x*y | x <- [100..999], y <- [100..999], x <= y ]
-- その中で結果が6桁になるもの
[ x*y | x <- [100..999], y <- [100..999], x <= y, 100000 <= x*y, x*y <= 999999 ]
-- その中で回文積になるもの
[ x*y | x <- [100..999], y <- [100..999], x <= y, 100000 <= x*y, x*y <= 999999, show (x*y) == reverse(show (x*y)) ]
-- その中で最大のもの
Data.List.maximum [ x*y | x <- [100..999], y <- [100..999], x <= y, 100000 <= x*y, x*y <= 999999, show (x*y) == reverse(show (x*y)) ]
-- ちなみにその時のxとy
Data.List.maximum [ (x*y, x, y) | x <- [100..999], y <- [100..999], x <= y, 100000 <= x*y, x*y <= 999999, show (x*y) == reverse(show (x*y)) ]
-- おまけ
-- 二乗の数が6桁の回文数となる数
[ (x*x, x) | x <- [100..999], 100000 <= x*x, x*x <= 999999, show (x*x) == reverse(show (x*x)) ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment