My blog. Like it? Subscribe to my feed or check out the archives.

Project Euler: problems 4-8 19 Jan 09

More Haskell, more Project Euler. Again I thought I would share my solutions to the next few problems. I should point out in advance that, for the most part, these are naive solutions. I plan on taking a second pass later to devise more clever approaches.

Problem 4

answer = maximum . filter isPalindrome $ products
    where products = [ x*y | x <- [111..999], y <- [x..999] ]
          isPalindrome n = n == reverse n 0
            where reverse 0 r = r
                  reverse n r = reverse (n `div` 10) (r * 10 + n `mod` 10)

Problem 5

answer = head . filter good $ [20,40..]
    where good n = not $ any (\t -> n `rem` t /= 0) [2..19]

Problem 6

answer = squareSum - sumSquare
    where squareSum = (sum [1..100]) ^ 2
          sumSquare = sum $ map (^2) [1..100]

Problem 7

primes :: [Integer]
primes = 2:3:primes'
    where 1:p:candidates = [6*k+r | k <- [0..], r <- [1,5]]
          primes'        = p : filter isPrime candidates
          isPrime n      = all (not . divides n) $ takeWhile (\p-> p*p <= n) primes'
          divides n p    = n `mod` p == 0
answer = primes !! 10001

Problem 8

import Char
largestDigitProduct d n = maximum products
    where products = [ digitProduct x digits | x <- [0..995]]
          digitProduct t ds = product $ take d (drop t ds)
          digits = map digitToInt (show n)
answer = largestDigitProduct 5 <number>

Post a comment