Returning the common divisors of two integers I have written this function that returns the common divisors of two integers. When called recursively 10 times with big integers ranging in 100000000, it takes over 5 seconds. How can I improve it so that it is faster (less than 5 seconds)?

commonDivisors :: Int -&gt; Int -&gt; [Int]
commonDivisors x y = if x &gt; y then divisors y x else divisors x y
    where divisors :: Int -&gt; Int -&gt; [Int]
          divisors z n = filter (\x -&gt; n `mod` x == 0 &amp;&amp; z `mod` x ==0) [1..n]
I don't know your application, but it seems rather uncommon to me that one would want a list of all common divisors.

To optimize your code, you could use Euclid's algorithm to find the greatest common divisor of the two numbers; this is much faster than iterating through all numbers less than your arguments.  Even more, the common divisors of your two numbers happen to be the divisors of the greatest common divisor.  Thus, the two-step process of finding the greatest common divisor and then finding all divisors of that greatest common divisor will be faster than your current method, since it does not usually have to consider all numbers up to the smaller factor.

An optimization for finding factors of a number \$n\$ would be only to iterate up to \$\sqrt n\$, adding to the list any factor \$k\$ you find as well as \$n / k\$.  A separate idea would be to compute the prime factorization of \$n\$ and finding the factors from that.