|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "code",
|
5 |
| - "execution_count": 1, |
| 5 | + "execution_count": 8, |
6 | 6 | "id": "8561e0a7",
|
7 | 7 | "metadata": {},
|
8 | 8 | "outputs": [
|
|
26 | 26 | },
|
27 | 27 | {
|
28 | 28 | "cell_type": "code",
|
29 |
| - "execution_count": 2, |
| 29 | + "execution_count": 7, |
30 | 30 | "id": "12f36b2f",
|
31 | 31 | "metadata": {},
|
32 | 32 | "outputs": [
|
|
36 | 36 | "21"
|
37 | 37 | ]
|
38 | 38 | },
|
39 |
| - "execution_count": 2, |
| 39 | + "execution_count": 7, |
40 | 40 | "metadata": {},
|
41 | 41 | "output_type": "execute_result"
|
42 | 42 | }
|
43 | 43 | ],
|
44 | 44 | "source": [
|
45 |
| - "def eucld_gcd(a,b):\n", |
46 |
| - " if(a < b):\n", |
47 |
| - " a , b = b , a\n", |
48 |
| - " if(b==0):\n", |
| 45 | + "def eucld_gcd(a, b):\n", |
| 46 | + " \"\"\"\n", |
| 47 | + " Computes the greatest common divisor (GCD) of two integers using the Euclidean algorithm.\n", |
| 48 | + "\n", |
| 49 | + " Parameters:\n", |
| 50 | + " a (int): The first integer.\n", |
| 51 | + " b (int): The second integer.\n", |
| 52 | + "\n", |
| 53 | + " Returns:\n", |
| 54 | + " int: The greatest common divisor of a and b.\n", |
| 55 | + " \"\"\"\n", |
| 56 | + " if a < b:\n", |
| 57 | + " a, b = b, a\n", |
| 58 | + " if b == 0:\n", |
49 | 59 | " return a\n",
|
50 |
| - " r = a%b\n", |
51 |
| - " if(r==0):\n", |
| 60 | + " r = a % b\n", |
| 61 | + " if r == 0:\n", |
52 | 62 | " return b\n",
|
53 |
| - " return eucld_gcd(b,r)\n", |
| 63 | + " return eucld_gcd(b, r)\n", |
| 64 | + "\n", |
54 | 65 | "eucld_gcd(252,105)"
|
55 | 66 | ]
|
56 | 67 | },
|
57 | 68 | {
|
58 | 69 | "cell_type": "code",
|
59 |
| - "execution_count": 3, |
| 70 | + "execution_count": 9, |
60 | 71 | "id": "c060ee17",
|
61 | 72 | "metadata": {},
|
62 | 73 | "outputs": [
|
|
66 | 77 | "[1, -48]"
|
67 | 78 | ]
|
68 | 79 | },
|
69 |
| - "execution_count": 3, |
| 80 | + "execution_count": 9, |
70 | 81 | "metadata": {},
|
71 | 82 | "output_type": "execute_result"
|
72 | 83 | }
|
73 | 84 | ],
|
74 | 85 | "source": [
|
75 | 86 | "import numpy as np\n",
|
76 |
| - "def ext_eucld(a,b):\n", |
| 87 | + "\n", |
| 88 | + "def ext_eucld(a, b):\n", |
| 89 | + " \"\"\"\n", |
| 90 | + " Computes the extended Euclidean algorithm to find the greatest common divisor (GCD)\n", |
| 91 | + " of two integers, and also the coefficients (x, y) of the equation:\n", |
| 92 | + " a*x + b*y = GCD(a, b)\n", |
| 93 | + "\n", |
| 94 | + " This method returns the coefficients (x, y) such that a*x + b*y = GCD(a, b).\n", |
| 95 | + "\n", |
| 96 | + " Parameters:\n", |
| 97 | + " a (int): The first integer.\n", |
| 98 | + " b (int): The second integer.\n", |
| 99 | + "\n", |
| 100 | + " Returns:\n", |
| 101 | + " list: A list of two integers [x, y] where x and y are the coefficients for the linear\n", |
| 102 | + " combination of a and b that equals their GCD.\n", |
| 103 | + " \"\"\"\n", |
77 | 104 | " swap = False\n",
|
78 |
| - " if(a < b):\n", |
79 |
| - " a , b = b , a\n", |
80 |
| - " swap = True\n", |
81 |
| - " def eucld(a,b):\n", |
82 |
| - " if(b==0 or b==1):\n", |
| 105 | + " if a < b:\n", |
| 106 | + " a, b = b, a\n", |
| 107 | + " swap = True\n", |
| 108 | + "\n", |
| 109 | + " def eucld(a, b):\n", |
| 110 | + " if b == 0 or b == 1:\n", |
83 | 111 | " return []\n",
|
84 | 112 | " ls = []\n",
|
85 |
| - " while b!=1:\n", |
86 |
| - " r = a%b\n", |
87 |
| - " if(r==0):\n", |
| 113 | + " while b != 1:\n", |
| 114 | + " r = a % b\n", |
| 115 | + " if r == 0:\n", |
88 | 116 | " return ls\n",
|
89 |
| - " idx = (a-r)//b\n", |
| 117 | + " idx = (a - r) // b\n", |
90 | 118 | " ls.append(idx)\n",
|
91 | 119 | " a = b\n",
|
92 | 120 | " b = r\n",
|
93 | 121 | " return ls\n",
|
94 |
| - " \n", |
95 |
| - " row = np.array([[1,0],[0,1]])\n", |
96 |
| - " ls = eucld(a,b)\n", |
| 122 | + "\n", |
| 123 | + " row = np.array([[1, 0], [0, 1]])\n", |
| 124 | + " ls = eucld(a, b)\n", |
97 | 125 | " for i in ls:\n",
|
98 |
| - " row = np.append(row, [row[-2] - i*row[-1]] ,axis=0)\n", |
99 |
| - " if(swap):\n", |
| 126 | + " row = np.append(row, [row[-2] - i * row[-1]], axis=0)\n", |
| 127 | + " \n", |
| 128 | + " if swap:\n", |
100 | 129 | " return list(row[-1])[::-1]\n",
|
| 130 | + " \n", |
101 | 131 | " return list(row[-1])\n",
|
102 |
| - "ext_eucld(97,2)" |
| 132 | + "\n", |
| 133 | + "ext_eucld(97, 2)\n" |
103 | 134 | ]
|
104 | 135 | },
|
105 | 136 | {
|
106 | 137 | "cell_type": "code",
|
107 |
| - "execution_count": 4, |
| 138 | + "execution_count": 10, |
108 | 139 | "id": "d3edd1f6",
|
109 | 140 | "metadata": {},
|
110 | 141 | "outputs": [
|
|
0 commit comments