152 lines
4.3 KiB
Plaintext
152 lines
4.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fb5ffc43",
|
|
"metadata": {},
|
|
"source": [
|
|
"# [Number Letter Counts](https://projecteuler.net/problem=17)\n",
|
|
"\n",
|
|
"Time to think about the rules for [English numerals](https://en.wikipedia.org/wiki/English_numerals), which you probably know intuitively but have never had to explain to a computer.\n",
|
|
"\n",
|
|
"First, we'll hard code some base cases:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "ba4aa4f0",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"numerals = {\n",
|
|
" 1: \"one\",\n",
|
|
" 2: \"two\",\n",
|
|
" 3: \"three\",\n",
|
|
" 4: \"four\",\n",
|
|
" 5: \"five\",\n",
|
|
" 6: \"six\",\n",
|
|
" 7: \"seven\",\n",
|
|
" 8: \"eight\",\n",
|
|
" 9: \"nine\",\n",
|
|
" 10: \"ten\",\n",
|
|
" 11: \"eleven\",\n",
|
|
" 12: \"twelve\",\n",
|
|
" 13: \"thirteen\",\n",
|
|
" 14: \"fourteen\",\n",
|
|
" 15: \"fifteen\",\n",
|
|
" 16: \"sixteen\",\n",
|
|
" 17: \"seventeen\",\n",
|
|
" 18: \"eighteen\",\n",
|
|
" 19: \"nineteen\",\n",
|
|
" 20: \"twenty\",\n",
|
|
" 30: \"thirty\",\n",
|
|
" 40: \"forty\",\n",
|
|
" 50: \"fifty\",\n",
|
|
" 60: \"sixty\",\n",
|
|
" 70: \"seventy\",\n",
|
|
" 80: \"eighty\",\n",
|
|
" 90: \"ninety\",\n",
|
|
" }"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8b11e55b",
|
|
"metadata": {},
|
|
"source": [
|
|
"Obviously, if a number is explicitly given in this dictionary, that's its English numeral. For any other two digit number, the numeral is just the concatenation of the numerals for the tens place and the ones place (e.g. 37 is \"thirty\" and \"seven\").\n",
|
|
"\n",
|
|
"For three digit numbers, we write the numeral for the hundreds place, followed by \"hundred\" (e.g. 300 is \"three hundred\"). If it's an exact multiple of 100 (e.g. 100, 200, etc.) then that's it. Otherwise, we stick on \"and\" and follow the rules for a two digit number for the remaining digits.\n",
|
|
"\n",
|
|
"For this problem, all that's left is a special case for 1,000. Here's our function:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "18b8dacb",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def numeral(num):\n",
|
|
" if num == 1000:\n",
|
|
" return \"onethousand\"\n",
|
|
" elif num in numerals:\n",
|
|
" return numerals[num]\n",
|
|
" elif num % 100 == 0:\n",
|
|
" return numerals[num // 100] + \"hundred\"\n",
|
|
" elif num > 100:\n",
|
|
" return numerals[num // 100] + \"hundredand\" + numeral(num % 100)\n",
|
|
" \n",
|
|
" ones = num % 10\n",
|
|
" rest = num - ones\n",
|
|
" return numerals[rest] + numerals[ones]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1e72e33b",
|
|
"metadata": {},
|
|
"source": [
|
|
"Now we can just use this to find all the numerals we need and sum their lengths."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "73b14cb1",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"21124"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"sum(len(numeral(n)) for n in range(1, 1001))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "417c208b",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Relevant sequences\n",
|
|
"* Number of letters in British numeral: [A362123](https://oeis.org/A362123)\n",
|
|
"\n",
|
|
"#### Copyright (C) 2025 filifa\n",
|
|
"\n",
|
|
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "SageMath 9.5",
|
|
"language": "sage",
|
|
"name": "sagemath"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|