Note that there are some explanatory texts on larger screens.

plurals
  1. POOut of Memory error finding products and primes
    text
    copied!<p>So I wrote a quick app to check my son's homework and it appears to be working fine. It takes a number, finds all the primes, then finds all the product combinations. So, for example, you pass it "210", and it will return:<br/> 2 x 3 x 5 x 7<br/> 5 x 6 x 7<br/> 7 x 30<br/> 6 x 35<br/> 5 x 42<br/> 3 x 7 x 10<br/> 10 x 21<br/> 3 x 70<br/> 3 x 5 x 14<br/> 14 x 15<br/> 2 x 7 x 15<br/> 2 x 105<br/> 2 x 5 x 21<br/> 2 x 3 x 35<br/></p> <p>The problem I'm having is when doing large numbers. It will handle 256 (2^8) after only a moment's hesitation, but when I do 512 (2^9), I get a OutOfMemoryException. Can someone recommend a better way of finding the product sets? My code is below.</p> <pre><code>class Program { private static List&lt;List&lt;int&gt;&gt; coll = new List&lt;List&lt;int&gt;&gt;(); public static List&lt;int&gt; PrimeFactors(int i) { if (i &lt; 2) { throw new ArgumentOutOfRangeException("Numbers less than 2 don't have prime factors"); } List&lt;int&gt; result = new List&lt;int&gt;(); int divisor = 2; while (divisor &lt;= i) { if (i % divisor == 0) { result.Add(divisor); i /= divisor; } else { divisor++; } } return result; } private static void GetFactors(List&lt;int&gt; l) { for (int i = 0; i &lt; l.Count - 1; i++) { for (int x = i + 1; x &lt; l.Count; x++) { List&lt;int&gt; loopList = new List&lt;int&gt;(l); int newProd = l[i] * l[x]; loopList.RemoveAt(i); loopList.RemoveAt(x-1); loopList.Add(newProd); loopList.Sort(); coll.Add(loopList); if (loopList.Count &gt; 2) { GetFactors(loopList); } } } } static void Main(string[] args) { int target = Convert.ToInt16(args[0]); List&lt;int&gt; results = PrimeFactors(target); results.Sort(); coll.Add(results); GetFactors(results); List&lt;string&gt; factors = new List&lt;string&gt;(); foreach (List&lt;int&gt; lst in coll) { string listString = ""; for (int i = 0; i &lt; lst.Count; i++) { if (i == lst.Count - 1) { listString += String.Format("{0}", lst[i]); } else { listString += String.Format("{0} x ", lst[i]); } } factors.Add(listString); } foreach (String factorString in factors.Select(x =&gt; x).Distinct()) { Console.WriteLine(factorString); } } } </code></pre> <p>EDIT: Here is the new code, based on answers below. Works for any Int16 I throw at it.</p> <pre><code> class Program { private static List&lt;List&lt;int&gt;&gt; coll = new List&lt;List&lt;int&gt;&gt;(); public static List&lt;int&gt; PrimeFactors(int i) { if (i &lt; 2) { throw new ArgumentOutOfRangeException("Numbers less than 2 don't have prime factors"); } List&lt;int&gt; result = new List&lt;int&gt;(); int divisor = 2; while (divisor &lt;= i) { if (i % divisor == 0) { result.Add(divisor); i /= divisor; } else { divisor++; } } return result; } private static void GetFactors(List&lt;int&gt; l) { for (int i = 0; i &lt; l.Count - 1; i++) { for (int x = i + 1; x &lt; l.Count; x++) { List&lt;int&gt; loopList = new List&lt;int&gt;(l); int newProd = l[i] * l[x]; loopList.RemoveAt(i); loopList.RemoveAt(x-1); loopList.Add(newProd); bool existsInCollection = false; foreach (List&lt;int&gt; existingList in coll) { if (ListEquality(existingList, loopList)) { existsInCollection = true; break; } } if (!existsInCollection) { coll.Add(loopList); if (loopList.Count &gt; 2) { GetFactors(loopList); } } } } } private static bool ListEquality(List&lt;int&gt; listA, List&lt;int&gt; listB) { if (listA.Count != listB.Count) return false; listA.Sort(); listB.Sort(); for (int idx = 0; idx &lt; listA.Count; idx++) { if (listA[idx] != listB[idx]) return false; } return true; } static void Main(string[] args) { int target = Convert.ToInt16(args[0]); List&lt;int&gt; results = PrimeFactors(target); results.Sort(); coll.Add(results); GetFactors(results); foreach (List&lt;int&gt; lst in coll) { string listString = ""; for (int i = 0; i &lt; lst.Count; i++) { if (i == lst.Count - 1) { listString += String.Format("{0}", lst[i]); } else { listString += String.Format("{0} x ", lst[i]); } } Console.WriteLine(listString); } } } </code></pre> <p>Thanks again to everyone.</p>
 

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