Skip to content

Instantly share code, notes, and snippets.

@NobukazuHanada
Forked from mizchi/codeiq.coffee
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NobukazuHanada/3984e46306d932e05142 to your computer and use it in GitHub Desktop.
Save NobukazuHanada/3984e46306d932e05142 to your computer and use it in GitHub Desktop.
# 問題: 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