title: Project Euler #23
body: <p>I'm currently trying to solve the project euler problem number 23 :</p>
  <blockquote>
  <p>A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.</p>
  <p>A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.</p>
  <p>As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.</p>
  <p>Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.</p>
  </blockquote>
  <p>However my code is not giving correct result while it seems to be completely fine. Am I calculating more than the enough numbers ?</p>
  <pre><code>private static void Main()
      {
          List&lt;int&gt; AbudantNumbers = new List&lt;int&gt;();
          long sum = 0;
          for (int i = 12; i &lt;= 28123; i++)
          {
              int abudantNumber = GetProperDivisor(i);
              if (abudantNumber &gt; i)
              {
                  AbudantNumbers.Add(i);
              }
          }
  
          for (int k = 1; k &lt;= 28123; k++)
          {
              int count = 0;
              for (int i = 0; i &lt; AbudantNumbers.Count; i++)
              {
                  count = 0;
                  if (AbudantNumbers[i] &gt; k)
                  {
                      break;
                  }
                  for (int j = i; j &lt; AbudantNumbers.Count; j++)
                  {
                      if (AbudantNumbers[j] &gt; k)
                      {
                          break;
                      }
                      if (AbudantNumbers[i] + AbudantNumbers[j] == k)
                      {
                          count++;
                          break;
                      }
                  }
              }
              if (count == 0)
              {
                  sum += k;
              }
          }
          Console.WriteLine(sum);
          Console.ReadKey();
      }
  
      private static int GetProperDivisor(int input)
      {
          int sum = 1;
          for (int i = 2; i &lt;= input / 2; i++)
          {
              if (input%i == 0)
              {
                  sum += i;
              }
          }
          return sum;
      }
  </code></pre>
  <p>My result is : 297632990
  Correct result is : 4179871</p>
  <p>Quite big difference when there are no obvious mistakes in my code.</p>
  <p>My second approach :</p>
  <pre><code>        for (int k = 1; k &lt;= 28123; k++)
          {
              var k1 = k;
              int count =
                  (from t1 in AbudantNumbers.TakeWhile(t1 =&gt; t1 &lt;= k1) let a = t1 select t1).Count(
                      t1 =&gt; AbudantNumbers.TakeWhile(t =&gt; t &lt;= k).Any(t =&gt; t1 + t == k));
              if (count == 0)
              {
                  sum += k;
              }
          }
  </code></pre>
  <p>My idea is to get all the abundant numbers lesser than 28123 than check all the integers lesser than 28123 (everything above have a sum of 2 abundant numbers) than rotate all the abundant numbers and lastly check if <code>abundantNumber1 + abundantNumber2 == currentNumber</code> if so we break out of the loop because we need only the ones that don't have a sum of 2 abundant numbers.</p>