Last active
July 29, 2017 15:51
-
-
Save vitkarpov/22f3ebead7652bceeedf869d82ab6890 to your computer and use it in GitHub Desktop.
Repeat in log n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Возвращает строку длины 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