Some fun algorithm in coffeescript/javascript