Solved 64.
This commit is contained in:
@@ -13,11 +13,61 @@
|
||||
"collapsed": true
|
||||
},
|
||||
"source": [
|
||||
"The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.\n",
|
||||
"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,
|
||||
@@ -29,7 +79,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "",
|
||||
"completion_date": "Sun, 6 Jan 2019, 06:17",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python3.6",
|
||||
@@ -47,7 +97,9 @@
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.5"
|
||||
},
|
||||
"tags": []
|
||||
"tags": [
|
||||
"powers"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
|
||||
Reference in New Issue
Block a user