Generating functions for nth Collatz iteration