Problem:
Problem C:Champernowne Constant

Champernown constant is an irrational number represented in decimal by "0." followed by
concatenation of all positive integers in the increasing order. The first few digits of this constant
are: 0.123456789101112...

Your task is to write a program that outputs the K digits of Chapnernown constant starting at
the N-th place for given two natural numbers K and N.

Input

The input has multiple lines. Each line has two positive integers N and K (N ≤ 109, K ≤ 100)
separated by a space.

The end of input is indicated by a line with two zeros. This line should not be processed.

Output

For each line, output a line that contains the K digits.

Sample Input

4 5
6 7
0 0

Output for the Sample Input

45678
6789101

Solution in Scala (judge verdict: Accepted)

import scala.annotation.tailrec
import scala.io.StdIn._
object Main extends App {

  loop()
  @tailrec def loop(): Unit = {
    val Array(n, k) = readLine.trim.split(' ').map(_.toLong)
    if (n == 0 && k == 0) return
    println(count(n - 1, k))
    loop()
  }
  def count(rest: Long, k: Long, digit: Int = 1): String = {
    val d = countDigit(digit)
    if (d * digit >= rest) makeString(rest % digit, k, power(10, digit - 1) + rest / digit)
    else count(rest - d * digit, k, digit + 1)
  }
  def makeString(rest: Long, k: Long, start: Long): String = {
    (start to start + k).mkString.substring(rest.toInt, rest.toInt + k.toInt)
  }
  def countDigit(digit: Int): Long = {
    power(10, digit) - power(10, digit - 1)
  }
  def power(base: Long, exp: Int): Long = {
    exp match {
      case 0 ⇒ 1L
      case 1 ⇒ base
      case _ ⇒ power(base * base, exp / 2) * power(base, exp % 2)
    }
  }
}