#CJam, <s>46</s> 45 bytes ri),{Is___"69"_W%erW%=!\"6809"-!&IA%g&'.*N}fI I think it can be golfed a little more. Takes `n` from STDIN. [Try it online here][1] [1]: http://cjam.aditsu.net/