1
\$\begingroup\$

I've "solved" Project Euler Question 4 and I was wondering if I could make my answer more efficient (I'm using Project Euler to help me learn Haskell).

The problem reads:

Find the largest palindrome made from the product of two 3-digit numbers

Here is my solution

getMaxPalindrome = maximum[x*y | x<-[100..999], y<-[100..999], reverse(show(x*y)) ==show(x*y)]

All suggestions for improvement are appreciated!

asked Oct 20, 2014 at 6:00
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

First, since * is commutative, you can save 1/2 of your computation if you restrict yourself to cases where x >= y:

[ x*y | x<-[100..999], y<-[100..x], ... ]

Second, if you could generate all the products in a non-increasing list, you'd just be searching for the first element of such a list satisfying the predicate, which would also speed the search very much. See data-ordlist package, which implements many useful functions on sorted lists, in particular in your case you'll probably need unionBy or unionAllBy

answered Oct 20, 2014 at 7:15
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.