check number of factors to avoid bad_alloc