简体   繁体   中英

Project Euler #1 using Haskell

import Data.Set

euler :: Int
euler = sum [ x | x <- nums ]
    where
    nums = Data.Set.toList (Data.Set.union (Data.Set.fromList [3,6..999])
                                           (Data.Set.fromList [5,10..999]))

I am learning Haskell and hope you don't mind me asking this. Is there a nicer way to get a list holding all natural numbers below one thousand that are multiples of 3 or 5? (Eg with zip or map?)

Edit:

import Data.List

euler :: Int
euler = sum (union [3,6..999] [5,10..999])

Thanks for your help, guys.

使用列表理解:

sum [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0]

您也可以使用硬编码版本:

sum $ [3, 6 .. 999] ++ [5, 10 .. 999] ++ [-15, -30 .. -999]

这将为您提供您要求的列表:

filter (\x -> (x `mod` 3 == 0) || (x `mod` 5 == 0)) [1..999]

Here's one.

mults35 = union [3,6..999] [5,10..999]
  where
    union (x:xs) (y:ys) = case (compare x y) of 
       LT -> x : union  xs  (y:ys)
       EQ -> x : union  xs     ys 
       GT -> y : union (x:xs)  ys
    union  xs     []    = xs
    union  []     ys    = ys

Here's another, less efficient way:

import Data.List

nub . sort $ ([3,6..999] ++ [5,10..999])

(we don't have to use fully qualified names if we have the import statement).

Also interesting is to find the multiples of only 3 and 5:

m35 = 1 : (map (3*) m35 `union` map (5*) m35)
sum [x | x <- [1..999], let m k = (x`mod`k==0), m 3 || m 5]

A more general solution for a list of numbers instead of just 3 and 5:

addMultiples :: [Int] ->  Int -> Int
addMultiples multiplesOf upTo = sum[n | n <- [1..upTo-1], or (map ((0==) . mod n) multiplesOf)]

Here's one that is super-fast. Try it with values of over a billion.

eu x = sum[div (n*(p*(p+1))) 2 | n<-[3,5,-15], let p = div (x-1) n]

I imagine it can be shortened further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM