Skip to the content

Digits to number the pages

Problem

In the scriptorium every digit is written by hand in costly ink. Before he starts, the copyist wants to know how much ink it will take to number the pages of the codex, from 1 to the last one.

Write a function that takes n, the number of pages (a whole number from 0 up), and returns how many digits he writes in total numbering them from 1 to n. With n equal to 0 he writes none: return 0. Counting page by page works, but grouping them by how many digits they take is better. With 120 pages there are 9 with one digit (1 to 9), 90 with two (10 to 99) and 21 with three (100 to 120), so the total is 9 + 180 + 63 = 252.

Examples

  • Ten pages

    10 → 11

  • The example

    120 → 252

  • A thousand pages

    1000 → 2893

  • A huge codex

    54321 → 260499

  • Almost a million

    999999 → 5888889

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def digits_needed(n):
    pass

JavaScript

function digitsNeeded(n) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.

More O(log n) challenges

See all challenges →