Skip to content

Instantly share code, notes, and snippets.

@vitkarpov
Last active July 29, 2017 15:51
Show Gist options
  • Save vitkarpov/22f3ebead7652bceeedf869d82ab6890 to your computer and use it in GitHub Desktop.
Save vitkarpov/22f3ebead7652bceeedf869d82ab6890 to your computer and use it in GitHub Desktop.
Repeat in log n
/**
* Возвращает строку длины n состоящую из символов s
* Имеет логарифмическую сложность.
*
* @param {String} s
* @returns {String}
*/
function repeat(s, n) {
let ans = s;
let len = 1;
while (n / len > 1) {
ans += ans;
n -= len;
len *= 2;
}
if (n > 1) {
ans += ans.substr(0, n - 1);
}
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment