Click here to Skip to main content
15,118,172 members
Articles / Programming Languages / C++
Tip/Trick
Posted 14 Jan 2020

Tagged as

Stats

1.6K views
97 downloads
3 bookmarked

Spell That Number

Rate me:
Please Sign up or sign in to vote.
2.43/5 (5 votes)
14 Jan 2020MIT1 min read
How to spell even large numbers in English

Sample Image

Introduction

fivethirtyeight.com is Nate Silver's site of charts and statistics that I visit periodically looking at a certain graph in hope the that it's going to change. They also publish occasional riddles and last week's riddle seemed interesting enough.

Here is the short version: spell a number in English and find its value by adding the value of all letters (a=1, b=2, c=3 and so on). Find the largest number that is smaller than its value.

The problem of spelling a number didn't look complicated but a short Google search gave me only awful implementations so I decided to roll my own.

Spelling a Number

The function spell takes care of spelling a number. For numbers less than 20, it just takes their names from a table. Numbers up to 100 get their names by combining the tens name ("twenty", "thirty", etc.) with the units name. Hundreds get treated the same way, then thousands, millions, billions and so on and so forth.

The function calls itself recursively and in general, it's a nice little programming exercise.

Riddles

What about the original question? The answer is 279! Run the program without any arguments to get the answer. It's larger than 42 but equally important.

To finish, here is another riddle: the alphanumeric value of a number grows much slower than the number itself. Can you find a best fitting function that approximates the alphanumeric value?

Points of Interest

None whatsoever! As far as I can see, there is no practical application of this code. It was however a fun thing to do on a wintry Sunday evening.

History

  • 13th January, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Mircea Neacsu
Canada Canada
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --