I've recently taken to learning me a haskell and have been having a pretty good time. I've been working through some Project Euler problems to get a grip of the syntax and have been reviewing the solutions posted here http://www.haskell.org/haskellwiki/Euler_problems/1_to_10 as a learning tool. Though I have found myself unable to wrap my head around the solution posted for problem #3:
-- Find the largest prime factor of 317584931803.
primes = 2 : filter ((==1) . length . primeFactors) [3,5..]
primeFactors n = factor n primes
where
factor n (p:ps)
| p*p > n = [n]
| n `mod` p == 0 = p : factor (n `div` p) (p:ps)
| otherwise = factor n ps
problem_3 = last (primeFactors 317584931803)
I can't figure out for the life of me how it works. primes
and primeFactors
seem to be calling each other to build their own lists and trying to follow it pickles my brain. Any one know a good blog post about this solution or want to write an explanation about it here?
This is indeed puzzling at first sight. But it's no magic if you don't think "imperatively". Haskell definitions are just that: the tell you what something is, not what lower level operations the computer is supposed to execute.
Hence, the list of primes is the list that contains 2 and all odd natural numbers greater 2 that have only a single prime factor (namely itself).
The list of prime factors for some integer n, on the other hand, is the list of prime numbers that divide n.
Make sure you understand those definitions, before reading on.
As abstract Haskell may be, it is still a programming language, so we need to give some advice from time to time how to compute something. Specifically, in the example above, we do not test all prime numbers to find prime factors of n
, because it is enough to test 2..k
where k*k <= n
.
This also makes sure that we only use that part of prime which is already computed.
At the start, primes
looks like this:
2 : filter ((==1) . length . primeFactors) [3,5..]
If we demand the next element after 2, we force evaluation of the expression right of the colon. This, in turn, causes filter to evaluate the predicate for 3. Then it goes:
primeFactors 3
factor 3 (2 : ...)
2*2 > 3
[3]
[3]
Hence, primeFactors 3
is [3]
and we didn't need to look beyond 2 in primes. (This is the key point why this works!)
Clearly, the length of [3]
is 1 and
2 : filter ((==1) . length . primeFactors) [3,5..]
evaluates to
2 : 3 : filter ((==1) . length . primeFactors) [5, 7..]
You may now want to reduce primeFactors 5
for yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With