## Project Euler 17: Count how many letters would be needed to write all the numbers in words from 1 to 1000.

#### Project Euler 17 Problem Description

Project Euler 17: 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.

#### Analysis

This version will deviate somewhat from the specified requirements and instead write out the numbers to 1 trillion as is required by the HackerRank Project Euler 17 version. The intention of the original Project Euler problem was to verify, somewhat, that the numbers were converted properly and uses a character count to verify a correct result.

Given that there is no other way of verifying that every number was correctly converted to words this is an OK compromise, but having a program that reliably translates numbers to words seems much more valuable. Such as spelling out monetary amounts on checks.

### Looking at the process

The grouping of the number into three digit groups is accomplished by the following line:

number = map(''.join, zip(*[iter(str(n).zfill(len(thousands)*3))]*3))

The intention is to always have a fully qualified number with leading zeros. For example, with the maximum `thousands`

of ‘Quadrillion’ or 10^{15}, 34,509 will become 000 000 000 000 034 509 (spaces added for readability).

Next, a Python idiom for clustering a data series into n-length groups using `zip(*[iter(s)]*n)`

, is applied to the zero-filled number. The final list named `number`

is compiled by the `map/join`

functions as: `['000', '000', '000', '000', '034', '509']`

. The ‘000’ groups are ignored during processing.

### Making the formatted number conform to other styles

Dashes can be added by including a dash at the end of each `tens`

list element other than ‘ten’. The extra spaces would have to be removed as changing ‘- ‘ to ‘-‘.

Likewise, a single ‘and’ could also be included after the thousands/hundreds group when the last group is less than 100.

The program is able to be expanded to include other thousand groups by adding the group name onto the code>thousands list if 999 quadrillion is not enough. I have no idea why, except for a projected U.S. deficit.

#### Project Euler 17 Solution

Runs < 0.001 seconds in Python 2.7.Use this link to get the Project Euler 17 Solution Python 2.7 source.

#### Answer

Slowly swipe from either end beginning with the white vertical bar to get an idea of the starting or ending digits. For less drama, just double click the answer area. The distance between the two bars will give you an idea of the magnitude. Touch devices can tap and hold the center of the box between the two bars and choose*define*to reveal the answer.

#### Afterthoughts

- Other thousands groups: Quintillion 10
^{18}, Sextillion 10^{21}, Septillion 10^{24}, Octillion 10^{27}, check here for more.

*Project Euler 17 Solution last updated*

## Discussion

## No comments yet.