var input = 344; var len = 1; var pos = 0; for(var i = 1; i <= 50000000; i++){ pos = (pos+input)%len + 1; len++; if(pos == 1){ console.log(i); } }