Snippet

project_euler/40.rb
Code:
# Project Euler Problem 40: Champernowne's Constant

# https://projecteuler.net/problem=40

# An irrational decimal fraction is created by concatenating the cardinals:
#   0.12345678910[1]112131415161718192021...
# It can be seen that the 12th digit of the fractional part is 1.
# If d(n) represents the nth 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)


def d(target)
    n, run = 0, 0
    while run < target
        n = (n.to_i + 1).to_s
        return n[target - run - 1].to_i if run + n.size >= target
        run += n.size
    end
end

def solve(n)
    (1..n).map { |e| d(10 ** e) }.inject(:*)
end

puts solve(6)
Output:
210