ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 23 May 2016 06:39:17 -0500List of prime factors with repetitionhttp://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/Is there a simple command on Sage wich gives, in place of factor(720)= 2^4 * 3^2 * 5, the list [2, 2, 2, 2, 3, 3, 5] of prime factors with repetition?Sat, 21 May 2016 09:14:39 -0500http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/Answer by slelievre for <p>Is there a simple command on Sage wich gives, in place of factor(720)= 2^4 * 3^2 * 5, the list [2, 2, 2, 2, 3, 3, 5] of prime factors with repetition?</p>
http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?answer=33517#post-id-33517Some potentially useful variations, building on previous answers and comments by @calc314 and @logomath.
The function `factor` returns a factorization.
sage: F = factor(2016)
sage: F
2^5 * 3^2 * 7
This secretly wraps a list of pairs (prime, multiplicity), and you can get this list.
sage: list(F)
[(2, 5), (3, 2), (7, 1)]
You can also iterate through this list of pairs directly.
sage: for pm in F: print pm
(2, 5)
(3, 2)
(7, 1)
This means you can use list comprehension in smart ways.
sage: [f[0] for f in F for _ in range(f[1])]
[2, 2, 2, 2, 2, 3, 3, 7]
Same as above, but calling (p, m) the pairs.
sage: [p for (p, m) in F for _ in range(m)]
[2, 2, 2, 2, 2, 3, 3, 7]
Getting a list of repetitions of each prime.
sage: [([p] * m) for (p, m) in F]
[[2, 2, 2, 2, 2], [3, 3], [7]]
Flattening it to get the list of prime factors with multiplicity.
sage: flatten([([p] * m) for (p, m) in F])
[2, 2, 2, 2, 2, 3, 3, 7]
Using `add` to sum lists starting from the empty list
(included as an optional parameter to `add`).
sage: add([[p] * m for (p, m) in F], [])
[2, 2, 2, 2, 2, 3, 3, 7]
(Edit.) Same but using an iterator instead of a list.
sage: add(([p] * m for (p, m) in F), [])
[2, 2, 2, 2, 2, 3, 3, 7]
Sun, 22 May 2016 20:48:30 -0500http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?answer=33517#post-id-33517Comment by logomath for <p>Some potentially useful variations, building on previous answers and comments by <a href="/users/218/calc314/">@calc314</a> and <a href="/users/8364/logomath/">@logomath</a>.</p>
<p>The function <code>factor</code> returns a factorization.</p>
<pre><code>sage: F = factor(2016)
sage: F
2^5 * 3^2 * 7
</code></pre>
<p>This secretly wraps a list of pairs (prime, multiplicity), and you can get this list.</p>
<pre><code>sage: list(F)
[(2, 5), (3, 2), (7, 1)]
</code></pre>
<p>You can also iterate through this list of pairs directly.</p>
<pre><code>sage: for pm in F: print pm
(2, 5)
(3, 2)
(7, 1)
</code></pre>
<p>This means you can use list comprehension in smart ways.</p>
<pre><code>sage: [f[0] for f in F for _ in range(f[1])]
[2, 2, 2, 2, 2, 3, 3, 7]
</code></pre>
<p>Same as above, but calling (p, m) the pairs.</p>
<pre><code>sage: [p for (p, m) in F for _ in range(m)]
[2, 2, 2, 2, 2, 3, 3, 7]
</code></pre>
<p>Getting a list of repetitions of each prime.</p>
<pre><code>sage: [([p] * m) for (p, m) in F]
[[2, 2, 2, 2, 2], [3, 3], [7]]
</code></pre>
<p>Flattening it to get the list of prime factors with multiplicity.</p>
<pre><code>sage: flatten([([p] * m) for (p, m) in F])
[2, 2, 2, 2, 2, 3, 3, 7]
</code></pre>
<p>Using <code>add</code> to sum lists starting from the empty list
(included as an optional parameter to <code>add</code>).</p>
<pre><code>sage: add([[p] * m for (p, m) in F], [])
[2, 2, 2, 2, 2, 3, 3, 7]
</code></pre>
<p>(Edit.) Same but using an iterator instead of a list.</p>
<pre><code>sage: add(([p] * m for (p, m) in F), [])
[2, 2, 2, 2, 2, 3, 3, 7]
</code></pre>
http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?comment=33522#post-id-33522Thank you, very interesting how sage is full of options. As I am using it for very large n, I remarked that the version " add([[p] ..." is nearly tenfold slower than the others (n near to 10000 digits).Mon, 23 May 2016 06:39:17 -0500http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?comment=33522#post-id-33522Answer by calc314 for <p>Is there a simple command on Sage wich gives, in place of factor(720)= 2^4 * 3^2 * 5, the list [2, 2, 2, 2, 3, 3, 5] of prime factors with repetition?</p>
http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?answer=33494#post-id-33494I don't know of a direct command. Here is one option, though.
F= factor(2016)
result=[f[0] for f in F for i in range(f[1])]
result
You may wish to look up the factor command for more details and examples.Sat, 21 May 2016 11:30:35 -0500http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?answer=33494#post-id-33494Comment by logomath for <p>I don't know of a direct command. Here is one option, though.</p>
<pre><code>F= factor(2016)
result=[f[0] for f in F for i in range(f[1])]
result
</code></pre>
<p>You may wish to look up the factor command for more details and examples.</p>
http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?comment=33509#post-id-33509I just add here a lambda-version of it for reference:
result = lambda n: [f[0] for f in factor(n) for i in range(f[1])]; result(2016)Sun, 22 May 2016 04:07:31 -0500http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?comment=33509#post-id-33509Comment by logomath for <p>I don't know of a direct command. Here is one option, though.</p>
<pre><code>F= factor(2016)
result=[f[0] for f in F for i in range(f[1])]
result
</code></pre>
<p>You may wish to look up the factor command for more details and examples.</p>
http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?comment=33506#post-id-33506Thank you, seems niceSun, 22 May 2016 01:03:45 -0500http://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/?comment=33506#post-id-33506