euler/ipython/EulerProblem063.ipynb

107 lines
2.2 KiB
Plaintext
Raw Normal View History

2018-12-29 10:17:35 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Powerful digit counts (Euler Problem 63)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
2019-01-22 06:12:10 +01:00
"The 5-digit number, 16807=$7^5$, is also a fifth power. Similarly, the 9-digit number, 134217728=$8^9$, is a ninth power.\n",
2018-12-29 10:17:35 +01:00
"\n",
"How many n-digit positive integers exist which are also an nth power?"
]
},
2019-01-22 06:12:10 +01:00
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def get_digit_count(n):\n",
" if n < 10:\n",
" return 1\n",
" c = 0\n",
" while n:\n",
" n //= 10\n",
" c += 1\n",
" return c\n",
"\n",
"assert(get_digit_count(0) == 1)\n",
"assert(get_digit_count(1) == 1)\n",
"assert(get_digit_count(33) == 2)\n",
"assert(get_digit_count(100) == 3)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"49\n"
]
}
],
"source": [
"def get_n_digit_positive_integers(n):\n",
" r = []\n",
" i = 1\n",
" while True:\n",
" if get_digit_count(i ** n) == n:\n",
" r.append(i ** n)\n",
" if get_digit_count(i ** n) > n:\n",
" return r\n",
" i += 1\n",
"\n",
"s = sum([len(get_n_digit_positive_integers(n)) for n in range(1, 1000)])\n",
"print(s)\n",
"assert(s == 49)"
]
},
2018-12-29 10:17:35 +01:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
2019-01-22 06:12:10 +01:00
"completion_date": "Sun, 6 Jan 2019, 06:17",
2018-12-29 10:17:35 +01:00
"kernelspec": {
"display_name": "Python 3",
"language": "python3.6",
"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.5"
},
2019-01-22 06:12:10 +01:00
"tags": [
"powers"
]
2018-12-29 10:17:35 +01:00
},
"nbformat": 4,
"nbformat_minor": 2
}