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...
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?
Problem 17: Number Letter Counts
Mathematical Development
Definition 1 (Atomic letter counts). Define the following lookup tables:
| Range | Symbol | Values | Sum |
|---|---|---|---|
| , | |||
| , | |||
| , |
Theorem 1 (Recursive decomposition of ). For :
where denotes the Iverson bracket, the constant counts the letters in “hundred”, and counts those in “and”.
Proof. This follows directly from British English number-naming rules:
- Numbers 1—9 are single words with letter counts .
- Numbers 10—19 (“ten” through “nineteen”) are single words with letter counts .
- 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 letters.
Lemma 1 (Sum over 1—9). .
Proof. .
Lemma 2 (Sum over 10—19). .
Proof. .
Lemma 3 (Sum over 20—99). .
Proof. Partition into eight decades for . In decade , the tens-word appears in all 10 numbers, contributing . The ones-words appear once each (for the non-zero units digit), contributing per decade. Summing over all 8 decades:
Theorem 2 (Sum over 1—99).
Proof. Sum of Lemmas 1, 2, and 3.
Theorem 3 (Sum over a single hundred-block). For a hundreds digit , define . Then
Proof. Each of the 100 numbers in begins with “[h] hundred”, contributing letters per number. For the 99 numbers with , the word “and” (3 letters) and the representation of are appended. The remaining number has no suffix. Therefore:
Theorem 4 (Total sum 1—1000).
Proof. Partition into , , and .
- (Theorem 2).
- (Theorem 3).
- (Theorem 1).
Total: .
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 to . 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 time and space.
Proof. The function executes a fixed number of comparisons, lookups, and arithmetic operations, each in time. The main loop calls exactly times. Storage is bounded by three constant-size lookup tables.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""Project Euler Problem 17: Number Letter Counts"""
def letter_count(n):
ones = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
teens = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
tens = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
if n == 1000:
return 11
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
if 10 <= n <= 19:
c += teens[n - 10]
n = 0
if 1 <= n <= 9:
c += ones[n]
return c
def main():
print(sum(letter_count(n) for n in range(1, 1001)))