All Euler problems
Project Euler

Number Letter Counts

Let ell(n) denote the number of letters (excluding spaces and hyphens) in the British English representation of the positive integer n. Compute sum_(n=1)^1000 ell(n). Convention. British English us...

Source sync Apr 19, 2026
Problem #0017
Level Level 00
Solved By 165,323
Languages C++, Python
Answer 21124
Length 404 words
modular_arithmeticrecursionarithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

If the numbers \(1\) to \(5\) are written out in words: one, two, three, four, five, then there are \(3 + 3 + 5 + 4 + 4 = 19\) letters used in total.

If all the numbers from \(1\) to \(1000\) (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, \(342\) (three hundred and forty-two) contains \(23\) letters and \(115\) (one hundred and fifteen) contains \(20\) letters. The use of "and" when writing out numbers is in compliance with British usage.

Problem 17: Number Letter Counts

Mathematical Development

Definition 1 (Atomic letter counts). Define the following lookup tables:

RangeSymbolValuesSum
ones[k]\text{ones}[k], k=1,,9k = 1,\ldots,9oko_k3,3,5,4,4,3,5,5,43,3,5,4,4,3,5,5,4SO=36S_O = 36
teens[k]\text{teens}[k], k=10,,19k = 10,\ldots,19tkt_k3,6,6,8,8,7,7,9,8,83,6,6,8,8,7,7,9,8,8ST=70S_T = 70
tens[k]\text{tens}[k], k=2,,9k = 2,\ldots,9dkd_k6,6,5,5,5,7,6,66,6,5,5,5,7,6,6SD=46S_D = 46

Theorem 1 (Recursive decomposition of \ell). For 1n10001 \le n \le 1000:

(n)={onif 1n9,tnif 10n19,dn/10+(nmod10)[nmod100]if 20n99,on/100+7+(3+(nmod100))[nmod1000]if 100n999,11if n=1000,\ell(n) = \begin{cases} o_n & \text{if } 1 \le n \le 9, \\ t_n & \text{if } 10 \le n \le 19, \\ d_{\lfloor n/10 \rfloor} + \ell(n \bmod 10) \cdot [n \bmod 10 \ne 0] & \text{if } 20 \le n \le 99, \\ o_{\lfloor n/100 \rfloor} + 7 + \bigl(3 + \ell(n \bmod 100)\bigr) \cdot [n \bmod 100 \ne 0] & \text{if } 100 \le n \le 999, \\ 11 & \text{if } n = 1000, \end{cases}

where [][\cdot] denotes the Iverson bracket, the constant 77 counts the letters in “hundred”, and 33 counts those in “and”.

Proof. This follows directly from British English number-naming rules:

  • Numbers 1—9 are single words with letter counts oko_k.
  • Numbers 10—19 (“ten” through “nineteen”) are single words with letter counts tkt_k.
  • Numbers 20—99 are composed of a tens-word optionally followed by a ones-word.
  • Numbers 100—999 have the form “[ones] hundred” optionally followed by “and [1—99]”.
  • The number 1000 is “one thousand” with 3+8=113 + 8 = 11 letters. \square

Lemma 1 (Sum over 1—9). Σ19=SO=36\Sigma_{1\text{--}9} = S_O = 36.

Proof. 3+3+5+4+4+3+5+5+4=363 + 3 + 5 + 4 + 4 + 3 + 5 + 5 + 4 = 36. \square

Lemma 2 (Sum over 10—19). Σ1019=ST=70\Sigma_{10\text{--}19} = S_T = 70.

Proof. 3+6+6+8+8+7+7+9+8+8=703 + 6 + 6 + 8 + 8 + 7 + 7 + 9 + 8 + 8 = 70. \square

Lemma 3 (Sum over 20—99). Σ2099=10SD+8SO=748\Sigma_{20\text{--}99} = 10 \cdot S_D + 8 \cdot S_O = 748.

Proof. Partition {20,,99}\{20, \ldots, 99\} into eight decades {10j,,10j+9}\{10j, \ldots, 10j+9\} for j=2,,9j = 2, \ldots, 9. In decade jj, the tens-word djd_j appears in all 10 numbers, contributing 10dj10 \cdot d_j. The ones-words o1,,o9o_1, \ldots, o_9 appear once each (for the non-zero units digit), contributing SOS_O per decade. Summing over all 8 decades:

Σ2099=10j=29dj+8k=19ok=1046+836=460+288=748.\Sigma_{20\text{--}99} = 10 \sum_{j=2}^{9} d_j + 8 \sum_{k=1}^{9} o_k = 10 \cdot 46 + 8 \cdot 36 = 460 + 288 = 748. \quad \square

Theorem 2 (Sum over 1—99).

Σ199=SO+ST+10SD+8SO=36+70+748=854.\Sigma_{1\text{--}99} = S_O + S_T + 10 \cdot S_D + 8 \cdot S_O = 36 + 70 + 748 = 854.

Proof. Sum of Lemmas 1, 2, and 3. \square

Theorem 3 (Sum over a single hundred-block). For a hundreds digit h{1,,9}h \in \{1, \ldots, 9\}, define Bh={100h,100h+1,,100h+99}B_h = \{100h, 100h+1, \ldots, 100h+99\}. Then

nBh(n)=100oh+1007+993+Σ199=100oh+1851.\sum_{n \in B_h} \ell(n) = 100 \cdot o_h + 100 \cdot 7 + 99 \cdot 3 + \Sigma_{1\text{--}99} = 100 \cdot o_h + 1851.

Proof. Each of the 100 numbers in BhB_h begins with “[h] hundred”, contributing oh+7o_h + 7 letters per number. For the 99 numbers 100h+r100h + r with 1r991 \le r \le 99, the word “and” (3 letters) and the representation of rr are appended. The remaining number 100h100h has no suffix. Therefore:

nBh(n)=100(oh+7)+993+Σ199=100oh+700+297+854=100oh+1851.\sum_{n \in B_h} \ell(n) = 100(o_h + 7) + 99 \cdot 3 + \Sigma_{1\text{--}99} = 100 \cdot o_h + 700 + 297 + 854 = 100 \cdot o_h + 1851. \quad \square

Theorem 4 (Total sum 1—1000).

n=11000(n)=Σ199+h=19(100oh+1851)+11=854+100SO+91851+11=21124.\sum_{n=1}^{1000} \ell(n) = \Sigma_{1\text{--}99} + \sum_{h=1}^{9} (100 \cdot o_h + 1851) + 11 = 854 + 100 \cdot S_O + 9 \cdot 1851 + 11 = 21124.

Proof. Partition {1,,1000}\{1, \ldots, 1000\} into {1,,99}\{1, \ldots, 99\}, B1,B2,,B9B_1, B_2, \ldots, B_9, and {1000}\{1000\}.

  • Σ199=854\Sigma_{1\text{--}99} = 854 (Theorem 2).
  • h=19(100oh+1851)=100SO+91851=3600+16659=20259\sum_{h=1}^{9}(100 \cdot o_h + 1851) = 100 \cdot S_O + 9 \cdot 1851 = 3600 + 16659 = 20259 (Theorem 3).
  • (1000)=11\ell(1000) = 11 (Theorem 1).

Total: 854+20259+11=21124854 + 20259 + 11 = 21124. \square

Editorial

We precompute the letter counts for the atomic words one through nineteen and the tens words twenty through ninety. A helper letterCount(n) decomposes each number according to British usage rules for hundreds and the word and, and the main loop sums that value for n=1n = 1 to NN. This is sufficient because every number in the range falls into one of the cases covered by the lookup-based decomposition.

Pseudocode

Algorithm: Letter Count for the Written Numbers from 1 to N
Require: An integer N with 1 ≤ N ≤ 1000.
Ensure: The total number of letters used when writing 1, 2, ..., N in words under British usage.
1: Prepare lookup tables for the atomic words up to nineteen, the tens words, and the fixed words "hundred", "and", and "thousand".
2: Initialize total ← 0.
3: For each n in {1, 2, ..., N}, decompose n into its thousands, hundreds, tens, and units parts and evaluate its British letter count from the lookup tables.
4: Update total ← total + letterCount(n).
5: Return total.

Complexity Analysis

Proposition 1. The algorithm runs in Θ(N)\Theta(N) time and Θ(1)\Theta(1) space.

Proof. The function (n)\ell(n) executes a fixed number of comparisons, lookups, and arithmetic operations, each in O(1)O(1) time. The main loop calls \ell exactly NN times. Storage is bounded by three constant-size lookup tables. \square

Answer

21124\boxed{21124}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_017/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int letterCount(int n) {
    static const int ones[] = {0, 3, 3, 5, 4, 4, 3, 5, 5, 4};
    static const int teens[] = {3, 6, 6, 8, 8, 7, 7, 9, 8, 8};
    static const int tens[] = {0, 0, 6, 6, 5, 5, 5, 7, 6, 6};

    if (n == 1000) return 11;
    int c = 0;
    if (n >= 100) {
        c += ones[n / 100] + 7;
        n %= 100;
        if (n > 0) c += 3;
    }
    if (n >= 20) {
        c += tens[n / 10];
        n %= 10;
        c += ones[n];
    } else if (n >= 10) {
        c += teens[n - 10];
    } else {
        c += ones[n];
    }
    return c;
}

int main() {
    int total = 0;
    for (int i = 1; i <= 1000; i++)
        total += letterCount(i);
    cout << total << endl;
    return 0;
}