Skip to content

Commit 7becd05

Browse files
committed
Update galois_field.ipynb
1 parent f3f32ae commit 7becd05

File tree

6 files changed

+2635
-0
lines changed

6 files changed

+2635
-0
lines changed

algorithms/Graph.ipynb

Lines changed: 946 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 8,
6+
"id": "8561e0a7",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"data": {
11+
"text/html": [
12+
"<style>.container{width:100%}</style>\n"
13+
],
14+
"text/plain": [
15+
"<IPython.core.display.HTML object>"
16+
]
17+
},
18+
"metadata": {},
19+
"output_type": "display_data"
20+
}
21+
],
22+
"source": [
23+
"%%HTML\n",
24+
"<style>.container{width:100%}</style>"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 5,
30+
"id": "12f36b2f",
31+
"metadata": {},
32+
"outputs": [
33+
{
34+
"data": {
35+
"text/plain": [
36+
"21"
37+
]
38+
},
39+
"execution_count": 5,
40+
"metadata": {},
41+
"output_type": "execute_result"
42+
}
43+
],
44+
"source": [
45+
"def eucld_gcd(a, b):\n",
46+
" \"\"\"\n",
47+
" Computes the greatest common divisor (GCD) of \n",
48+
" two integers using the Euclidean algorithm.\n",
49+
"\n",
50+
" Parameters:\n",
51+
" a (int): The first integer.\n",
52+
" b (int): The second integer.\n",
53+
"\n",
54+
" Returns:\n",
55+
" int: The greatest common divisor of a and b.\n",
56+
" \"\"\"\n",
57+
" if a < b:\n",
58+
" a, b = b, a\n",
59+
" if b == 0:\n",
60+
" return a\n",
61+
" r = a % b\n",
62+
" if r == 0:\n",
63+
" return b\n",
64+
" return eucld_gcd(b, r)\n",
65+
"\n",
66+
"\n",
67+
"eucld_gcd(252, 105)"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 6,
73+
"id": "c060ee17",
74+
"metadata": {},
75+
"outputs": [
76+
{
77+
"data": {
78+
"text/plain": [
79+
"[1, -48]"
80+
]
81+
},
82+
"execution_count": 6,
83+
"metadata": {},
84+
"output_type": "execute_result"
85+
}
86+
],
87+
"source": [
88+
"import numpy as np\n",
89+
"\n",
90+
"\n",
91+
"def ext_eucld(a, b):\n",
92+
" \"\"\"\n",
93+
" Computes the extended Euclidean algorithm to find the \n",
94+
" greatest common divisor (GCD)of two integers, and \n",
95+
" also the coefficients (x, y) of the equation:\n",
96+
" a*x + b*y = GCD(a, b)\n",
97+
"\n",
98+
" This method returns the coefficients (x, y) \n",
99+
" such that a*x + b*y = GCD(a, b).\n",
100+
"\n",
101+
" Parameters:\n",
102+
" a (int): The first integer.\n",
103+
" b (int): The second integer.\n",
104+
"\n",
105+
" Returns:\n",
106+
" list: A list of two integers [x, y] where x and y are the \n",
107+
" coefficients for the linear combination of a and b \n",
108+
" that equals their GCD.\n",
109+
" \"\"\"\n",
110+
" swap = False\n",
111+
" if a < b:\n",
112+
" a, b = b, a\n",
113+
" swap = True\n",
114+
"\n",
115+
" def eucld(a, b):\n",
116+
" if b in {0,1}:\n",
117+
" return []\n",
118+
" ls = []\n",
119+
" while b != 1:\n",
120+
" r = a % b\n",
121+
" if r == 0:\n",
122+
" return ls\n",
123+
" idx = (a - r) // b\n",
124+
" ls.append(idx)\n",
125+
" a = b\n",
126+
" b = r\n",
127+
" return ls\n",
128+
"\n",
129+
" row = np.array([[1, 0], [0, 1]])\n",
130+
" ls = eucld(a, b)\n",
131+
" for i in ls:\n",
132+
" row = np.append(row, [row[-2] - i * row[-1]], axis=0)\n",
133+
"\n",
134+
" if swap:\n",
135+
" return list(row[-1])[::-1]\n",
136+
"\n",
137+
" return list(row[-1])\n",
138+
"\n",
139+
"\n",
140+
"ext_eucld(97, 2)"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 7,
146+
"id": "d3edd1f6",
147+
"metadata": {},
148+
"outputs": [
149+
{
150+
"name": "stdout",
151+
"output_type": "stream",
152+
"text": [
153+
"a=5, b=7, gcd=1, m=3, n=-2, ma+nb=1\n"
154+
]
155+
}
156+
],
157+
"source": [
158+
<<<<<<< HEAD
159+
"a, b= 5, 7\n",
160+
=======
161+
"a = 5\n",
162+
"b = 7\n",
163+
>>>>>>> ffbe3bbd8623bff453d16b942b7cf035c20d808c
164+
"gcd = eucld_gcd(a, b)\n",
165+
"m, n = ext_eucld(a, b)\n",
166+
"print(f\"a={a}, b={b}, gcd={gcd}, m={m}, n={n}, ma+nb={m*a+n*b}\")"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": null,
172+
"id": "782bb8d3",
173+
"metadata": {},
174+
"outputs": [],
175+
"source": []
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"id": "e224063e",
181+
"metadata": {},
182+
"outputs": [],
183+
"source": []
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": null,
188+
"id": "3da0aaac-23b4-4118-ae3e-aef611f0f38c",
189+
"metadata": {},
190+
"outputs": [],
191+
"source": []
192+
}
193+
],
194+
"metadata": {
195+
"kernelspec": {
196+
"display_name": "Python 3 (ipykernel)",
197+
"language": "python",
198+
"name": "python3"
199+
},
200+
"language_info": {
201+
"codemirror_mode": {
202+
"name": "ipython",
203+
"version": 3
204+
},
205+
"file_extension": ".py",
206+
"mimetype": "text/x-python",
207+
"name": "python",
208+
"nbconvert_exporter": "python",
209+
"pygments_lexer": "ipython3",
210+
"version": "3.12.3"
211+
}
212+
},
213+
"nbformat": 4,
214+
"nbformat_minor": 5
215+
}

0 commit comments

Comments
 (0)