Prime factorization algorithm