euler/ipython/EulerProblem063.ipynb

107 lines
2.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Powerful digit counts (Euler Problem 63)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"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",
"\n",
"How many n-digit positive integers exist which are also an nth power?"
]
},
{
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "Sun, 6 Jan 2019, 06:17",
"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"
},
"tags": [
"powers"
]
},
"nbformat": 4,
"nbformat_minor": 2
}