I think foaly's is the simplest working code.
Although I think instead of:
count=count%9
You should replace it with:
count= (count==9 ? 0:count);
Because you don't want to have unnecessarily large integers being stored in memory.
EDIT: actually ignore this, foaly's does exactly the same
My mistake.