Pyth, 22 19 bytes
3 bytes thanks to Jakube.
seSs.:RQ.n,L_MdCB.z
If we are allowed to print leading zeroes, the code is 18 bytes:
eSs.:RQ.n,L_MdCB.z
3 bytes thanks to Jakube.
seSs.:RQ.n,L_MdCB.z
If we are allowed to print leading zeroes, the code is 18 bytes:
eSs.:RQ.n,L_MdCB.z