title: Efficiently finding all divisors of a number
body: <p>So I simply want to find all the divisors of a given number (excepting the number itself).
  Currently, I have this:</p>
  
  <pre><code>public static List&lt;int&gt; proper_divisors(int x)
  {
      List&lt;int&gt; toreturn = new List&lt;int&gt;();
      toreturn.Add(1);
      int i = 0;
      int j=1;
      int z = 0;
      while (primes.ElementAt(i) &lt; Math.Sqrt(x))
      {
          if (x % primes.ElementAt(i) == 0)
          {
              toreturn.Add(primes.ElementAt(i));
              toreturn.Add(x / primes.ElementAt(i));
              j = 2;
              z = (int)Math.Pow(primes.ElementAt(i), 2);
              while (z &lt; x)
              {
                  if (x % z == 0)
                  {
                      toreturn.Add(z);
                      toreturn.Add(x / z);
                      j++;
                      z = (int)Math.Pow(primes.ElementAt(i), j);
                  }
                  else
                  {
                      z = x;
                  }
              }
          }
          i++;
      }
      toreturn = toreturn.Distinct().ToList&lt;int&gt;();
      return toreturn;
  }
  </code></pre>
  
  <p>where primes is a list of primes (assume it is correct, and is large enough).
  The algorithm works in the sense that it finds all the prime factors, but not all the factors (i.e. given 34534, it returns {1,2,17267,31,1114} but misses {62, 557} as 62 is a combination, and therefore misses 557 as well.</p>
  
  <p>I have also tried just getting the prime factors of a number, but I don't know how to convert that into a list of all of the correct combinations.</p>
  
  <p>The code for that algorithm is as follows:</p>
  
  <pre><code>public static List&lt;int&gt; prime_factors(int x)
  {
      List&lt;int&gt; toreturn = new List&lt;int&gt;();
      int i = 0;
      while (primes.ElementAt(i) &lt;= x)
      {
          if (x % primes.ElementAt(i) == 0)
          {
              toreturn.Add(primes.ElementAt(i));
              x = x / primes.ElementAt(i);
          }
          else
          {
              i++;
          }
      }
      return toreturn;
  }
  </code></pre>
  
  <p>Any ideas on how to fix the first one, or how to create the list of combinations from the second one (I would prefer that as it would be faster)?</p>