Project Euler Problem 40
問題
An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If d(n) represents the n'th digit of the fractional part, find the value of the following expression. d(1) × d(10) × d(100) × d(1000) × d(10000) × d(100000) × d(1000000)
ソース
@pos = [1] (1...8).each{|i| @pos << @pos[i - 1] + i * (10 ** (i - 1)) * 9} def solv(n) a = @pos.find_all{|x| x <= n} index, x = a.last, 10 ** (a.size - 1) num = x + (n - index) / x.to_s.size num.to_s[(n - index) % x.to_s.size].to_i end||< **解答 >|| 210
感想
1000000文字まで数字を連結しても、それほど(?)時間がかからないが、
頑張って計算で算出しました!
コレはなかなか良い問題です。
@posは10^nが何文字目から始まるかを計算してあり、そこを起点に算出しています。