# Factor polynomial

**URL:** <https://discourse.julialang.org/t/factor-polynomial/110926>\
**Category:** General Usage\
**Tags:** polynomials, nemo, factorization\
**Created:** [February 28, 2024, 11:56pm UTC](https://discourse.julialang.org/t/factor-polynomial/110926 "2024-02-28T23:56:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 28, 2024, 11:56pm UTC](https://discourse.julialang.org/t/factor-polynomial/110926/1 "2024-02-28T23:56:25Z")

</div>

I want to `factor(x^5 + x + 1)` into `(x^3 - x^2 + 1)(x^2 + x + 1)`. Wolfram can do it, is there a Julia package that can?

AbstractAlgebra.jl has a function called `factor` but I can’t figure out how to use it.

Polynomials.jl has a FactoredPolynomial but it doesn’t do what I’m looking for either

```julia
julia> convert(FactoredPolynomial, Polynomial([1,0,0,0,1,1]))
FactoredPolynomial((x + 0.5000000000000002 + 0.8660254037844388im) * (x - 0.6623589786223729 - 0.5622795120623012im) * (x + 1.3247179572447456 - 5.605801581444737e-17im) * (x + 0.5000000000000001 - 0.8660254037844386im) * (x - 0.662358978622373 + 0.5622795120623013im))

```

---

<div class="post-metadata">

**Author:** ![sumiya11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sumiya11/32/207147_2.png) [@sumiya11](https://discourse.julialang.org/u/sumiya11)\
**Post date:** [February 29, 2024, 5:04am UTC](https://discourse.julialang.org/t/factor-polynomial/110926/2 "2024-02-29T05:04:58Z")

</div>

I would do the following:

```julia
julia> using Nemo

Welcome to Nemo version 0.43.0

Nemo comes with absolutely no warranty whatsoever

julia> R, x = QQ["x"]
(Univariate polynomial ring in x over QQ, x)

julia> factor(x^5 + x + 1)
1 * (x^3 - x^2 + 1) * (x^2 + x + 1)

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [May 8, 2025, 12:01am UTC](https://discourse.julialang.org/t/factor-polynomial/110926/3 "2025-05-08T00:01:52Z")

</div>

Is there a way to get irrational coefficients?

I want to `factor(a^4 + b^4)` into `(a^2 + b^2 + √2*a*b) * (a^2 + b^2 - √2*a*b)`.

Or imaginary coefficients, to `factor(a^2 + b^2)` into `(a - im*b)(a + im*b))`?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [May 8, 2025, 3:48am UTC](https://discourse.julialang.org/t/factor-polynomial/110926/4 "2025-05-08T03:48:37Z")

</div>

> [@jar1](#):
>
> I want to `factor(a^4 + b^4)` into `(a^2 + b^2 + √2*a*b) * (a^2 + b^2 - √2*a*b)`.

Don’t have time rn to try out a specific example, but perhaps the functionality is here:

- [Algebraic numbers · Nemo.jl](https://nemocas.github.io/Nemo.jl/dev/algebraic/)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 8, 2025, 6:41am UTC](https://discourse.julialang.org/t/factor-polynomial/110926/5 "2025-05-08T06:41:11Z")

</div>

Linking a [related post](https://discourse.julialang.org/t/computer-algebra-factor-function/102837/2) with a solution using Hecke.jl.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [May 8, 2025, 1:43pm UTC](https://discourse.julialang.org/t/factor-polynomial/110926/6 "2025-05-08T13:43:32Z")

</div>

Here the complete example:

```julia
julia> using Hecke

julia> Qx, x = QQ[:x]; K, sqrt2 = number_field(x^2 - 2, "√2");

julia> Kab, (a, b) = K[:a, :b];

julia> factor(a^4 + b^4)
1 * (a^2 - √2*a*b + b^2) * (a^2 + √2*a*b + b^2)

```

This works if you know the “irrationalities” that will pop up.

There is also a model to work with the set of all algebraic numbers (roots of things etc).

```julia
julia> K = algebraic_closure(QQ);

julia> Kab, (a, b) = K[:a, :b];

julia> factor(a^2 + b^2)
1 * (a + Root -1.00000*im of x^2 + 1*b) * (a + Root 1.00000*im of x^2 + 1*b)

```

(But the printing is not nice. This is `(a - im*b)*(a - im*b)`, but it prints `im` as `Root -1.0... of x^2 + 1`.)
