You are not logged in.
Pages: 1
So I've been learning haskell this past week, and I thought it might be a good idea to get some feedback from guys who know what they're doin' . I just finished euler #4 here, so if anyone can see anything that could be implemented in a better way, stylistic changes, etc. or maybe my code is just completely unreadable and I should work on that first xD.
But yeah, much appreciated
[home page] -- [code / configs]
"Once you go Arch, you must remain there for life or else Allan will track you down and break you." -- Bregol
Offline
I didn't try your code and it might be more efficient, but I solved it with a lot less code:
main =
print $ maximum $ filter isPalindrome' [ x*y | x<-[1..999], y<-[1..999]]
isPalindrome' :: (Integral a) => a -> Bool
isPalindrome' = isPalindrome . digitsRev
isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome x = x == reverse x
digitsRev :: (Integral a) => a -> [a]
digitsRev 0 = []
digitsRev n = remainder : digitsRev quotient
where (quotient, remainder) = quotRem n 10
This was a while ago though, and I made it pretty quick and didn't bother optimizing as it runs in less than a second
Offline
I tried a similar solution to yours (although it was in lua) where I just ran through every possibility and just took the largest palindrome, but it took at least a minute to run, so I had to figure out a way to list all the products in order, and return the first one found.
actually your code above runs for a minute and produces a stack overflow for me. maybe my machine just sucks balls, lol.
Last edited by chris-kun (2011-01-23 22:04:44)
[home page] -- [code / configs]
"Once you go Arch, you must remain there for life or else Allan will track you down and break you." -- Bregol
Offline
I tried a similar solution to yours (although it was in lua) where I just ran through every possibility and just took the largest palindrome, but it took at least a minute to run, so I had to figure out a way to list all the products in order, and return the first one found.
actually your code above runs for a minute and produces a stack overflow for me. maybe my machine just sucks balls, lol.
Really? Did you compile it with -O2 ? It runs in about 0.40 seconds on my core 2 duo at 1.8GHz, not such an exceptionally powerful machine I'd think...
Offline
ah you're right compiled it runs in 2.3 secs for me, although mine still runs in 0.043
[home page] -- [code / configs]
"Once you go Arch, you must remain there for life or else Allan will track you down and break you." -- Bregol
Offline
Yeah your solution is probably way more efficient
I just tried the brute force method first and it was fast enough
Offline
nvm, speedbump in my brain...
Last edited by dagle (2011-01-29 21:05:46)
Offline
I rather like my approach: https://github.com/Daenyth/Project-Eule … /euler4.hs
[git] | [AURpkgs] | [arch-games]
Offline
yeah I get that you can bruteforce it.. I was trying to add in the functionality of ordering all of the products from largest to smallest, though
[home page] -- [code / configs]
"Once you go Arch, you must remain there for life or else Allan will track you down and break you." -- Bregol
Offline
Pages: 1