Final code to compute factorization