My friend is doing project euler in scala and he's up to #32. He's ten times the programmer I am so I had better get back to work. I did the first 46 in erlang but as of problem 47 I switched to haskell. That should mean I'd have to re-implement many of the utility functions I already wrote in erlang, except that I'm "borrowing" anything I already wrote in erlang so the language jump doesn't hurt too badly.
I'm just starting to wrap my head around haskell so if you must ridicule my code at least suggest improvements.
-- #47
p47 = p47try (map compress_factors factorizations)
p47try list =
__if__ all (\x -> length x >= 4) seq && -- each has 4+ factors
sum (map length seq) == length (nub $ concat seq) -- all factors distinct
__then__ map unfactorize seq
__else__ p47try $ tail list
__where__ seq = take 4 list Note that factorizations is an infinite list:
factorizations = map factorise [2..]
compress_factors [] = []
compress_factors xs =
(head(xs), length top) **:** compress_factors (drop (length top) xs)
__where__ top = [x | x <- xs, x == head(xs) ]
unfactorize xs =
product [a^b | (a,b) <- xs] That's my favorite part of haskell, that you can have data structures that represent infinite lists. Since everything is evaluated lazily, it's happy to let you work with infinite structures - but if you ask for the last element of the list you may have to wait a bit. Grab some coffee.
Sorry that some of the syntax coloring is hard to read - this is the default HsColour html output. I'll see if I can find something more readable.
Update: problem 48. One liners are nice.
-- #48
p48 = sum [x^x | x <- [1..1000] ] `mod` 10^10 |
walterzuey(Permanent Link)factorise

unfactorize

This should cause a SOWPODS memory dump.

herooftheage(Permanent Link)Fortunately, google is up to the task of letting me figure out what you're talking about.

extempore(Permanent Link)olaugh(Permanent Link)shunny(Permanent Link)Thanks.

kvom01(Permanent Link)Now have solved 115 out of 183 using Java (solved a few using Excel). Good entertainment.

I was a programmer my whole career, so the algorithms are not a big problem. However, understanding some of the math principles can be a challenge. Mathworld site is definitely a help.