# Factoring Polynomials with SymPy

Here is an example that uses [SymPy](http://sympy.org/en/index.html) to factor polynomials.


```
from IPython.html.widgets import interact
from IPython.display import display
```


```
from sympy import Symbol, Eq, factor, init_printing
init_printing(use_latex='mathjax')
```


```
x = Symbol('x')
```


```
def factorit(n):
    display(Eq(x**n-1, factor(x**n-1)))
```

Notice how the output of the `factorit` function is properly formatted LaTeX.


```
factorit(12)
```


$$x^{12} - 1 = \left(x - 1\right) \left(x + 1\right) \left(x^{2} + 1\right) \left(x^{2} - x + 1\right) \left(x^{2} + x + 1\right) \left(x^{4} - x^{2} + 1\right)$$



```
interact(factorit, n=(2,40));
```


$$x^{21} - 1 = \left(x - 1\right) \left(x^{2} + x + 1\right) \left(x^{6} + x^{5} + x^{4} + x^{3} + x^{2} + x + 1\right) \left(x^{12} - x^{11} + x^{9} - x^{8} + x^{6} - x^{4} + x^{3} - x + 1\right)$$



```

```

d492e3ce68d20569a51c6062d9927adf7b5ab9d1
ipynb
Jupyter Notebook
101notebook/ipython-rel-2.1.0-examples/Interactive Widgets/Factoring.ipynb
OpenBookProjects/ipynb
72a28109e8e30aea0b9c6713e78821e4affa2e33
MIT
2015-06-08T12:50:14.000Z
2018-11-20T10:05:01.000Z
101notebook/ipython-rel-2.1.0-examples/Interactive Widgets/Factoring.ipynb
OpenBookProjects/ipynb
72a28109e8e30aea0b9c6713e78821e4affa2e33
MIT
101notebook/ipython-rel-2.1.0-examples/Interactive Widgets/Factoring.ipynb
OpenBookProjects/ipynb
72a28109e8e30aea0b9c6713e78821e4affa2e33
MIT
2016-01-26T14:12:50.000Z
2021-02-20T14:24:09.000Z
Qwen/Qwen-72B
1. YES
2. YES