% pdflatex --shell-escape -synctex=1 -interaction=nonstopmode %.tex|python C:\Users\trash\AppData\Local\Programs\MiKTeX\scripts\pythontex\pythontex.py %.tex|pdflatex --shell-escape -synctex=1 -interaction=nonstopmode %.tex|"C:/Program Files (x86)/Adobe/Acrobat 11.0/Acrobat/Acrobat.exe" %.pdf	
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{pythontex}


\begin{pycode}
def decompose (n,table=False):
    factors_list = []
    i=2
    val = n
    while n>1:
        exposant = 0
        while n%i == 0:
            exposant = exposant + 1
            n = n/i
        if exposant != 0:
            factors_list.append([i,exposant])
        i = i+1

    if table == False:
        decomp = str(factors_list[0][0])+'^'+str(factors_list[0][1])
        for i in range(1,len(factors_list)):
            if factors_list[i][1] != 0:
                decomp = decomp + '\\times' + str(factors_list[i][0])+'^'+str(factors_list[i][1])
        return '$'+decomp+'$'
    else:
        result = '\\begin{tabular}{r|l}'
        n = val
        for prime in factors_list:
            for f in range(prime[1]):
                result = result+str(n)+'&'+str(prime[0])+'\\\\'
                n = n//prime[0]
        return result + '1&\\\\\\end{tabular}'
\end{pycode}

\newcommand{\ifactors}[2][]{
\ifx&#1&
\begingroup\edef\x{\endgroup
    \noexpand\py{decompose(#2)}}\x
\else
\begingroup\edef\x{\endgroup
    \noexpand\py{decompose(#2,True)}}\x
\fi}

\setlength{\parindent}{0pt}
\begin{document}
Décomposition en produit de facteurs premiers de 120 sous forme de table:
\begin{quote}
\ifactors[table]{120}
\end{quote}

\bigskip

Décomposition en produit de facteurs premiers de 120 en ligne:
\begin{quote}
\ifactors{120}
\end{quote}
\end{document}

