Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you already have a list of the prime factors, what you want to do is to compute the powerset of that list. </p> <p>Now, one problem is that you might have duplicates in the list (e.g. the prime factors of 20 = 2 * 2 * 5), but sets don't allow duplicates. So, we can make each element of the list unique by projecting it to a structure of the form {x, y} where x is the prime and y is the index of the prime in the list.</p> <pre><code>var all_primes = primes.Select((x, y) =&gt; new { x, y }).ToList(); </code></pre> <p>Now, <code>all_primes</code> is a list of the form {x, y} where x is the prime and y is the index in the list.</p> <p>Then we compute the power set (definition of <code>GetPowerSet</code> below):</p> <pre><code>var power_set_primes = GetPowerSet(all_primes); </code></pre> <p>Hence, <code>power_set_primes</code> is an <code>IEnumerable&lt;IEnumerable&lt;T&gt;&gt;</code> where <code>T</code> is the anonymous type <code>{x, y}</code> where x and y are of type <code>int</code>.</p> <p>Next, we compute the product of the each element in the power set</p> <pre><code>foreach (var p in power_set_primes) { var factor = p.Select(x =&gt; x.x).Aggregate(1, (x, y) =&gt; x * y); factors.Add(factor); } </code></pre> <p>Putting it all together:</p> <pre><code>var all_primes = primes.Select((x, y) =&gt; new { x, y }).ToList(); //assuming that primes contains duplicates. var power_set_primes = GetPowerSet(all_primes); var factors = new HashSet&lt;int&gt;(); foreach (var p in power_set_primes) { var factor = p.Select(x =&gt; x.x).Aggregate(1, (x, y) =&gt; x * y); factors.Add(factor); } </code></pre> <p>From <a href="http://rosettacode.org/wiki/Power_Set" rel="nofollow noreferrer">http://rosettacode.org/wiki/Power_Set</a> for implementations of powerset.</p> <pre><code>public IEnumerable&lt;IEnumerable&lt;T&gt;&gt; GetPowerSet&lt;T&gt;(List&lt;T&gt; list) { return from m in Enumerable.Range(0, 1 &lt;&lt; list.Count) select from i in Enumerable.Range(0, list.Count) where (m &amp; (1 &lt;&lt; i)) != 0 select list[i]; } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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