add problem 80
This commit is contained in:
parent
92992297b2
commit
edcd9b60d1
|
@ -0,0 +1,109 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "3aeab8bf",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# [Square Root Digital Expansion](https://projecteuler.net/problem=80)\n",
|
||||||
|
"\n",
|
||||||
|
"Unfortunately, just calling Python's `sqrt` won't cut it - floats don't have nearly enough digits of precision.\n",
|
||||||
|
"\n",
|
||||||
|
"The easiest approach here is to use SageMath's arbitrary precision routines, or Python's [decimal](https://docs.python.org/3/library/decimal.html) package. We need to watch for rounding issues, so we'll calculate slightly more than 100 digits and truncate."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "b1d58be8",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"40886"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"def digital_sum(n):\n",
|
||||||
|
" s = sqrt(n).n(digits=110)\n",
|
||||||
|
" digits = (int(d) for d in str(s)[:101] if d != '.')\n",
|
||||||
|
" return sum(digits)\n",
|
||||||
|
" \n",
|
||||||
|
"\n",
|
||||||
|
"sum(digital_sum(n) for n in range(1, 101) if not is_square(n))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "ad2b9aee",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"If you'd rather not use those tools for some reason, another simple approach is to take advantage of Python's arbitrary precision integers and calculate the [integer square root](https://en.wikipedia.org/wiki/Integer_square_root) of $n \\times 10^k$ for a sufficiently large $k$ ($k \\geq 200$ for this problem)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "6bd0cb0d",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"40886"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"def digital_sum(n):\n",
|
||||||
|
" s = isqrt(n * 10^200)\n",
|
||||||
|
" digits = (int(d) for d in str(s)[:100])\n",
|
||||||
|
" return sum(digits)\n",
|
||||||
|
" \n",
|
||||||
|
"\n",
|
||||||
|
"sum(digital_sum(n) for n in range(1, 101) if not is_square(n))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "c193ba51",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"If you want to go the extra mile, there are several [algorithms for computing square roots](https://en.wikipedia.org/wiki/Square_root_algorithms) that you can implement yourself, such as Heron's method, which is a special case of [Newton's method](https://en.wikipedia.org/wiki/Newton%27s_method) for solving $x^2 - n = 0$. The method works by starting with an initial estimate $x_0$ (such as $\\frac{n}{2}$), then repeatedly calculating\n",
|
||||||
|
"$$x_{k+1} = \\frac{1}{2}\\left(x_k + \\frac{n}{x_k}\\right)$$\n",
|
||||||
|
"until $|x_{k+1} - x_k|$ is sufficiently small. For computing the integer square root, this can be when $|x_{k+1} - x_k| < 1$."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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
|
||||||
|
}
|
Loading…
Reference in New Issue