[Haskell-beginners] list doesn't end

Francesco Ariis fa-ml at ariis.it
Mon Nov 25 13:20:24 UTC 2019


On Mon, Nov 25, 2019 at 08:55:17AM +0100, Alexander Chen wrote:
> Hi Francesco,
> 
> Their is a ^2, ^3, ^4 in the lists, respectively.
> 
> see https://projecteuler.net/problem=87

This runs reasonably fast even on my old 32bit machine:

    import qualified Data.Set as S
    import Data.Numbers.Primes

    sol72 :: Integer -> Int
    sol72 l = S.size . S.fromList $
                [(x+y+z) | x <- p2, y <- p3, z <- p4, x+y+z < l]
        where
              f :: Integer -> [Integer]
              f n = takeWhile (< l) $ map (^n) primes

              p2, p3, p4 :: [Integer]
              p2 = f 2
              p3 = f 3
              p4 = f 4

    λ> sol72 (50 * 10^6)
    1097343

Notice how the lengths for lists p2, p3 and p4 are actually 908, 73
and 23!
-F



More information about the Beginners mailing list