Sunday, May 23, 2010

Project Euler | Problem 4

This problem asks you to find the biggest 6-digit palindrome that is the product of two 3-digit numbers. I found this one quite boring, so I'll just put the code (in the functional language Amanda) I used to solve it here and not think about it too much.
condition x = (~.empty) [y | y <- [100..999] ; divides y x ; (x/y)<1000 ]
isPalindrome len val = (take len (itoa val))=((reverse.drop len) (itoa val))
pali6 = reverse [x| x <- [100000..999999]; isPalindrome 3 x]
answer = hd [x | x <- pali6 ; condition x]
divides y x = (x%y = 0)

Those familiar with haskell should probably be able to understand this. It takes about half a minute to evaluate, which is probably because I first calculate all the palindromes and then check them. Also, it should be possible to do the whole thing in one line, but it gets kinda hard to read that way.

No comments:

Post a Comment