Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Avoid using <code>ArrayList</code>, prefer generic collection such as <code>List&lt;Int64&gt;</code> because you conversion may be quite expensive.</p> <p>There is a part in your program which is in <em>O(n^3)</em>. It can be reduce to <em>O(n^2)</em> easily. Just replace the following code (<em>O(n)</em> because of <code>List&lt;T&gt;.Contains</code>):</p> <pre><code>if(listOfSums.Contains(num) == false) { listOfSums.Add(num); } </code></pre> <p>by:</p> <pre><code>listOfSums.Add(num); </code></pre> <p>where <code>listOfSums</code> is now a <code>HashSet&lt;Int64&gt;</code> (it's used to have a collection without duplicates).</p> <p>Moreover, you can reduce the time for populating the <code>listOfSums</code> by a factor of 2 by just noticing that if you exchange <code>i</code> and <code>a</code>, the <code>sum</code> is the same, then, only one of the two combinations is added to the <code>listOfSums</code>.</p> <p>You can replace:</p> <pre><code>for (int i = 1; i &lt; listOfAbundantNumbers.Count; i++) for (int a = 0; a &lt; listOfAbundantNumbers.Count; a++) </code></pre> <p>by;</p> <pre><code>for (int i = 1; i &lt; listOfAbundantNumbers.Count; i++) for (int a = 0; a &lt;= i; a++) </code></pre> <p>The final code is:</p> <pre><code>private static void Main() { List&lt;Int64&gt; listOfAbundantNumbers = new List&lt;Int64&gt;(); HashSet&lt;Int64&gt; listOfSums = new HashSet&lt;Int64&gt;(); long total = 0; for (int i = 1; i &lt; 20162; i++) { if (isAbundandt(i)) { listOfAbundantNumbers.Add(i); } total += i; } for (int i = 0; i &lt; listOfAbundantNumbers.Count; i++) for (int a = 0; a &lt;= i; a++) { long temp1 = Convert.ToInt64(listOfAbundantNumbers[i]); long temp2 = Convert.ToInt64(listOfAbundantNumbers[a]); long num = temp1 + temp2; listOfSums.Add(num); } for (int i = 1; i &lt; listOfAbundantNumbers.Count; i++) { long temp1 = Convert.ToInt64(listOfAbundantNumbers[i]); total -= temp1; } Console.WriteLine(total + ""); } private static List&lt;Int64&gt; divisorList(long input) { List&lt;Int64&gt; divisors = new List&lt;Int64&gt;(); for (long i = 2; i &lt; Math.Round(Math.Sqrt(input), 0, 0); i++) { long temp = input % i; if (temp == 0) { divisors.Add(i); divisors.Add(input / i); } } return divisors; } private static Boolean isAbundandt(long input) { long sum = 0; List&lt;Int64&gt; divisor = divisorList(input); for (int i = 0; i &lt; divisor.Count; i++) { long temp1 = divisor[i]; sum += temp1; } sum++; if (sum &gt; input) { return true; } return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload