Fast factorization in cpp