-
-
Save NobukazuHanada/3984e46306d932e05142 to your computer and use it in GitHub Desktop.
This file contains 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
# 問題: 5, 6, 7, 8, 9, 1, 0, 1, 1, 1, 2, 1, 3, ... | |
# という数列のn番目の数字を求める関数を書け | |
f = (n) -> n + 5 | |
solve = (index)-> | |
ret = "" | |
cnt = 0 | |
while true | |
ret += f(cnt++) | |
if ret.length > index | |
return ret[index] | |
solve2 = (index,n,acc) -> | |
if n == undefined | |
return solve2(index + 4,0,0) | |
next_n = n + 1 | |
next_acc = acc + 9 * next_n * Math.pow(10,n) | |
if index < next_acc | |
tmp = index - acc | |
mod = tmp % next_n | |
div = tmp / next_n | |
return (div + Math.pow(10,n)).toString().charAt(mod) | |
else | |
solve2(index,next_n,next_acc) | |
console.log [0..10].map (i) -> solve2 i | |
console.log [0..10].map (i) -> solve i | |
alert 'answer solve : ' + solve2(Math.pow(10,7)) | |
alert 'answer solve : ' + solve(Math.pow(10,7)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment