This can be significantly improved using another algorithm: the smallest number that can be divided into a set of numbers (in this case, the set [1..10]) is the smallest common multiple of these numbers.
Haskell even has a built-in function of least common function ( lcm ), which you can use:
Prelude> foldl lcm 1 [1..10] 2520
If you prefer not to use the built-in lcm function (as itβs almost a hoax :)), you can do this using the Euclidean algorithm to calculate the GCD, and then using:
lcm ab = a * b `div` gcd ab
If you need to find all the numbers in this list that are divisible by [1..n], you can use the fact that any such number will also be divisible by the least common multiple [1..n]:
divis n xs = filter (\x -> x `mod` mult == 0) xs where mult = foldl lcm 1 [1..n]
source share