ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 23 Sep 2016 15:03:59 -0500Number of factors of a polynomialhttps://ask.sagemath.org/question/34910/number-of-factors-of-a-polynomial/Given a polynomial f, the command `f.factor()` gives the factorization of f. I want to find out the number of factors of f. Is there any command for that ?
The polynomial ring is assumed to be `ZZ[x]`.Fri, 23 Sep 2016 10:41:06 -0500https://ask.sagemath.org/question/34910/number-of-factors-of-a-polynomial/Comment by FrédéricC for <p>Given a polynomial f, the command <code>f.factor()</code> gives the factorization of f. I want to find out the number of factors of f. Is there any command for that ?</p>
<p>The polynomial ring is assumed to be <code>ZZ[x]</code>.</p>
https://ask.sagemath.org/question/34910/number-of-factors-of-a-polynomial/?comment=34911#post-id-34911len(f.factor())Fri, 23 Sep 2016 11:19:20 -0500https://ask.sagemath.org/question/34910/number-of-factors-of-a-polynomial/?comment=34911#post-id-34911Answer by tmonteil for <p>Given a polynomial f, the command <code>f.factor()</code> gives the factorization of f. I want to find out the number of factors of f. Is there any command for that ?</p>
<p>The polynomial ring is assumed to be <code>ZZ[x]</code>.</p>
https://ask.sagemath.org/question/34910/number-of-factors-of-a-polynomial/?answer=34912#post-id-34912The factorization of a polynomial is an object with its methods:
sage: R.<x> = ZZ[]
sage: f = (x-1)^3*(x+2) ; f
x^4 - x^3 - 3*x^2 + 5*x - 2
sage: F = f.factor() ; F
(x + 2) * (x - 1)^3
sage: type(F)
<class 'sage.structure.factorization.Factorization'>
So, this objects has its methods, which you can call more or less directely:
sage: F.__len__()
2
sage: len(F)
2
sage: list(F)
[(x + 2, 1), (x - 1, 3)]
sage: dict(F)
{x - 1: 3, x + 2: 1}
You question is a bit ambiguous, since it is not clear whether you want the number of factors counting with or without cultiplicity.
Without multiplicity, you canjust call the length:
sage: len(F)
2
sage: len(dict(F))
2
With multiplicity, you have to sum the multipicities of all factors:
sage: dict(F).values()
[1, 3]
sage: sum(dict(F).values())
4
Fri, 23 Sep 2016 15:03:59 -0500https://ask.sagemath.org/question/34910/number-of-factors-of-a-polynomial/?answer=34912#post-id-34912