Skip to content

Commit cf0fddc

Browse files
committed
P-E-square root and Nthroot-edited-2
1 parent 7d9942d commit cf0fddc

2 files changed

+31
-22
lines changed

PE-Heron method for computing square root of positive number.ipynb

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"metadata": {},
77
"source": [
88
"# Python everything, exercise:\n",
9-
"## Heron's method for computing $\\sqrt x$\n",
10-
"**Heron's method** computes the *square root* of a given positive number $x$ by\n",
9+
"## Heron's method for computing $\\sqrt a$\n",
10+
"**Heron's method** computes the *square root* of a given positive number $a$ by\n",
1111
" 1) $x_0 \\leftarrow$ some initial guess <br>\n",
12-
" 2) for $n=0,1,2...$ do: <br>\n",
13-
"$\\;\\;\\;\\; x_{n+1}=\\frac{1}{2}(x_n+\\frac{x}{x_n})$\n",
12+
" 2) for $k=0,1,2...$ do: <br>\n",
13+
"$\\;\\;\\;\\;\\Large x_{k+1}=\\frac{1}{2}(x_k+\\frac{a}{x_k})$\n",
1414
"\n",
1515
"YouTube Channel: **@ostad-ai**\n",
1616
"<br>The Python code is at: https://github.com/ostad-ai/Python-Everything"
@@ -23,11 +23,11 @@
2323
"metadata": {},
2424
"outputs": [],
2525
"source": [
26-
"def heron(x,x0=1,iter=20):\n",
27-
" xn=x0\n",
28-
" for n in range(iter):\n",
29-
" xn=(xn+x/xn)/2\n",
30-
" return xn"
26+
"def heron(a,x0=1,iter=20):\n",
27+
" xk=x0\n",
28+
" for _ in range(iter):\n",
29+
" xk=(xk+a/xk)/2\n",
30+
" return xk"
3131
]
3232
},
3333
{
@@ -45,8 +45,8 @@
4545
}
4646
],
4747
"source": [
48-
"x=2\n",
49-
"print(f'Square root of {x} = {heron(x)}')"
48+
"a=2\n",
49+
"print(f'Square root of {a} = {heron(a)}')"
5050
]
5151
},
5252
{

PE-Nth root algorithm.ipynb

+20-11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"metadata": {},
77
"source": [
88
"# Python everything, exercise:\n",
9-
"## Nth root algorithm for computing $\\sqrt[n] x$\n",
10-
"**Nth root algorithm** computes the *nth root* of a given positive number $x$ by\n",
9+
"## Nth root algorithm for computing $\\sqrt[n] a$\n",
10+
"**Nth root algorithm** computes the *nth root* of a given positive number $a$ by\n",
1111
" 1) $x_0 \\leftarrow$ some initial guess <br>\n",
1212
" 2) for $k=0,1,2...$ do: <br>\n",
13-
"$\\;\\;\\;\\; x_{k+1}=\\frac{1}{n}((n-1)x_k+\\frac{x}{x^{n-1}_k})$\n",
13+
"$\\;\\;\\;\\;\\Large x_{k+1}=\\frac{1}{n}((n-1)x_k+\\frac{a}{x^{n-1}_k})$\n",
1414
"\n",
1515
"YouTube Channel: **@ostad-ai**\n",
1616
"<br>The Python code is at: https://github.com/ostad-ai/Python-Everything"
@@ -19,6 +19,21 @@
1919
{
2020
"cell_type": "code",
2121
"execution_count": 1,
22+
"id": "80c175d2",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"def nth_root(a,n=2,x0=1,iter=20):\n",
27+
" xk=x0\n",
28+
" n1=n-1\n",
29+
" for _ in range(iter):\n",
30+
" xk=(n1*xk+a/xk**n1)/n\n",
31+
" return xk"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 2,
2237
"id": "d757f8ac",
2338
"metadata": {},
2439
"outputs": [
@@ -31,15 +46,9 @@
3146
}
3247
],
3348
"source": [
34-
"def nth_root(x,n=2,x0=1,iter=20):\n",
35-
" xk=x0\n",
36-
" n1=n-1\n",
37-
" for _ in range(iter):\n",
38-
" xk=(n1*xk+x/xk**n1)/n\n",
39-
" return xk\n",
40-
"x=2\n",
49+
"a=2\n",
4150
"n=3\n",
42-
"print(f'{n}th root of {x} is:{nth_root(x,n)}')"
51+
"print(f'{n}th root of {a} is:{nth_root(a,n)}')"
4352
]
4453
},
4554
{

0 commit comments

Comments
 (0)