mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-22 01:02:04 +00:00
208 lines
4.5 KiB
Plaintext
208 lines
4.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "8561e0a7",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<style>.container{width:100%}</style>\n"
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.HTML object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"source": [
|
|
"%%HTML\n",
|
|
"<style>.container{width:100%}</style>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "12f36b2f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"21"
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"def eucld_gcd(a, b):\n",
|
|
" \"\"\"\n",
|
|
" Computes the greatest common divisor (GCD) of two integers using the Euclidean algorithm.\n",
|
|
"\n",
|
|
" Parameters:\n",
|
|
" a (int): The first integer.\n",
|
|
" b (int): The second integer.\n",
|
|
"\n",
|
|
" Returns:\n",
|
|
" int: The greatest common divisor of a and b.\n",
|
|
" \"\"\"\n",
|
|
" if a < b:\n",
|
|
" a, b = b, a\n",
|
|
" if b == 0:\n",
|
|
" return a\n",
|
|
" r = a % b\n",
|
|
" if r == 0:\n",
|
|
" return b\n",
|
|
" return eucld_gcd(b, r)\n",
|
|
"\n",
|
|
"\n",
|
|
"eucld_gcd(252, 105)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "c060ee17",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[1, -48]"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"\n",
|
|
"def ext_eucld(a, b):\n",
|
|
" \"\"\"\n",
|
|
" Computes the extended Euclidean algorithm to find the greatest common divisor (GCD)\n",
|
|
" of two integers, and also the coefficients (x, y) of the equation:\n",
|
|
" a*x + b*y = GCD(a, b)\n",
|
|
"\n",
|
|
" This method returns the coefficients (x, y) such that a*x + b*y = GCD(a, b).\n",
|
|
"\n",
|
|
" Parameters:\n",
|
|
" a (int): The first integer.\n",
|
|
" b (int): The second integer.\n",
|
|
"\n",
|
|
" Returns:\n",
|
|
" list: A list of two integers [x, y] where x and y are the coefficients for the linear\n",
|
|
" combination of a and b that equals their GCD.\n",
|
|
" \"\"\"\n",
|
|
" swap = False\n",
|
|
" if a < b:\n",
|
|
" a, b = b, a\n",
|
|
" swap = True\n",
|
|
"\n",
|
|
" def eucld(a, b):\n",
|
|
" if b == 0 or b == 1:\n",
|
|
" return []\n",
|
|
" ls = []\n",
|
|
" while b != 1:\n",
|
|
" r = a % b\n",
|
|
" if r == 0:\n",
|
|
" return ls\n",
|
|
" idx = (a - r) // b\n",
|
|
" ls.append(idx)\n",
|
|
" a = b\n",
|
|
" b = r\n",
|
|
" return ls\n",
|
|
"\n",
|
|
" row = np.array([[1, 0], [0, 1]])\n",
|
|
" ls = eucld(a, b)\n",
|
|
" for i in ls:\n",
|
|
" row = np.append(row, [row[-2] - i * row[-1]], axis=0)\n",
|
|
"\n",
|
|
" if swap:\n",
|
|
" return list(row[-1])[::-1]\n",
|
|
"\n",
|
|
" return list(row[-1])\n",
|
|
"\n",
|
|
"\n",
|
|
"ext_eucld(97, 2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "d3edd1f6",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"a=5, b=7, gcd=1, m=3, n=-2, ma+nb=1\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"a = 5\n",
|
|
"b = 7\n",
|
|
"gcd = eucld_gcd(a, b)\n",
|
|
"m, n = ext_eucld(a, b)\n",
|
|
"print(f\"a={a}, b={b}, gcd={gcd}, m={m}, n={n}, ma+nb={m*a+n*b}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "782bb8d3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e224063e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "63f1d5a4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"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.12.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|