factorize/3 is already yesterday’s snow, check out naish/2. But anything should work that doesn’t give a stack overflow.
factorize/3
naish/2