euler/ipython/EulerProblem030.ipynb

84 lines
1.9 KiB
Plaintext
Executable File

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 30\n",
"\n",
"Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:\n",
"\n",
"$1634 = 1^4 + 6^4 + 3^4 + 4^4$\n",
"\n",
"$8208 = 8^4 + 2^4 + 0^4 + 8^4$\n",
"\n",
"$9474 = 9^4 + 4^4 + 7^4 + 4^4$\n",
"\n",
"As $1 = 1^4$ is not a sum it is not included.\n",
"\n",
"The sum of these numbers is $1634 + 8208 + 9474 = 19316$.\n",
"\n",
"Find the sum of all the numbers that can be written as the sum of fifth powers of their digits."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is an eays brute force. We look up the fifth powers."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"443839\n"
]
}
],
"source": [
"fifth_power_lookup = {str(i): i**5 for i in range(0,10)}\n",
"\n",
"def is_number_sum_of_fiths_powers_of_digits(n):\n",
" return n == sum([fifth_power_lookup[d] for d in str(n)])\n",
"\n",
"s = sum([i for i in range(1, 1000000) if is_number_sum_of_fiths_powers_of_digits(i) and not i is 1])\n",
"print(s)\n",
"assert(s == 443839)"
]
}
],
"metadata": {
"completion_date": "Fri, 25 Aug 2017, 12:13",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.3"
},
"tags": [
"digit",
"powers",
"brute force"
]
},
"nbformat": 4,
"nbformat_minor": 2
}