{ "cells": [ { "cell_type": "code", "execution_count": 8, "id": "8561e0a7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%HTML\n", "" ] }, { "cell_type": "code", "execution_count": 5, "id": "12f36b2f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def eucld_gcd(a, b):\n", " \"\"\"\n", " Computes the greatest common divisor (GCD) of \n", " 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": 6, "id": "c060ee17", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, -48]" ] }, "execution_count": 6, "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 \n", " greatest common divisor (GCD)of two integers, and \n", " 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) \n", " 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 \n", " coefficients for the linear combination of a and b \n", " 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 in {0,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": 7, "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": [ <<<<<<< HEAD "a, b= 5, 7\n", ======= "a = 5\n", "b = 7\n", >>>>>>> ffbe3bbd8623bff453d16b942b7cf035c20d808c "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": "3da0aaac-23b4-4118-ae3e-aef611f0f38c", "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 }