| 1 |
% luagcd package |
| 2 |
% version 1.1 |
| 3 |
% Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt. |
| 4 |
% Authors: Chetan Shirore and Ajit Kumar |
| 5 |
|
| 6 |
\ProvidesPackage{luagcd}[1.1] |
| 7 |
\RequirePackage{luacode} |
| 8 |
\begin{luacode*} |
| 9 |
function findgcd2(a,b) -- function to find gcd of 2 numbers. |
| 10 |
a = math.abs(a) |
| 11 |
b = math.abs(b) |
| 12 |
if b ~= 0 then |
| 13 |
return findgcd2(b, a % b) |
| 14 |
else |
| 15 |
return a |
| 16 |
end |
| 17 |
end |
| 18 |
|
| 19 |
function findgcd(...) -- function to find gcd of 2 or more numbers. |
| 20 |
local tbl = table.pack(...) |
| 21 |
if #(tbl) > 2 then |
| 22 |
local rem = table.remove(tbl,1) |
| 23 |
return findgcd (rem, findgcd( table.unpack(tbl) ) ) |
| 24 |
else |
| 25 |
u,v = table.unpack(tbl) |
| 26 |
return math.floor(findgcd2(u,v)) |
| 27 |
end |
| 28 |
end |
| 29 |
|
| 30 |
function inputcheck ( ... ) -- validating input. |
| 31 |
local tbl = table.pack(...) |
| 32 |
for _, v in ipairs(tbl) do |
| 33 |
if type(v) ~= 'number' then |
| 34 |
error('Only numbers are expected.') |
| 35 |
return |
| 36 |
elseif v~= math.floor(v) then |
| 37 |
error('Error: Only integers are expected.') |
| 38 |
return |
| 39 |
end |
| 40 |
end |
| 41 |
end |
| 42 |
- function to find gcd with input validatiion. |
| 43 |
function luagcd(...) - |
| 44 |
inputcheck(...) |
| 45 |
return findgcd(...) |
| 46 |
|
| 47 |
end |
| 48 |
-- function to find gcd of 2 numbers with steps. |
| 49 |
function stepbystepgcd(a,b,sep) |
| 50 |
if type(a) ~= 'number' or type(b) ~= 'number' then |
| 51 |
error('Only numbers are expected.') |
| 52 |
return |
| 53 |
elseif a~= math.floor(a) or b~= math.floor(b) then |
| 54 |
error('Error: Only integers are expected.') |
| 55 |
return |
| 56 |
end |
| 57 |
local val1,val2 = a,b |
| 58 |
a,b = math.max(math.abs(a),math.abs(b)), |
| 59 |
math.min(math.abs(a),math.abs(b)) |
| 60 |
p,q = math.max(math.abs(a),math.abs(b)), |
| 61 |
math.min(math.abs(a),math.abs(b)) |
| 62 |
if b==0 then |
| 63 |
return |
| 64 |
("The gcd of " .. val1 .." and " .. val2 .. " is " .. a .. '.' ) |
| 65 |
end |
| 66 |
local tbl ={} |
| 67 |
local k = 0 |
| 68 |
local sep = sep or 'Step ' |
| 69 |
local stepcnt = 0 |
| 70 |
while b~=0 do |
| 71 |
x=a |
| 72 |
y=b |
| 73 |
t = b |
| 74 |
b = a % b |
| 75 |
a = t |
| 76 |
k=k+1 |
| 77 |
stepcnt = stepcnt + 1 |
| 78 |
if b ~=0 then |
| 79 |
d = x // y |
| 80 |
tbl[k] = sep .. stepcnt .. |
| 81 |
": Apply the division algorithm to " .. x .." and " .. y .."." |
| 82 |
k=k+1 |
| 83 |
tbl[k] = "$".. x.." = ".. t .."(" |
| 84 |
.. d ..") + ".. b .."$" |
| 85 |
else |
| 86 |
tbl[k] = sep .. stepcnt .. |
| 87 |
": Apply the division algorithm to " .. x .." and " .. y .."." |
| 88 |
k=k+1 |
| 89 |
tbl[k] ="$".. x.." = "..t.."(" |
| 90 |
.. (x // y)..") + ".. b .."$" |
| 91 |
end |
| 92 |
end |
| 93 |
local str = table.concat(tbl,"\\\\") |
| 94 |
if stepcnt==1 then |
| 95 |
return str .. " \\\\" .. "The gcd of ".. val1 .." and " .. val2.. |
| 96 |
" is " ..t.. "." |
| 97 |
|
| 98 |
else |
| 99 |
return str .. " \\\\" .. "The gcd of ".. val1 .." and " .. val2 .. |
| 100 |
" is the last non-zero remainder and it is " ..t.. "." |
| 101 |
end |
| 102 |
end |
| 103 |
-- function to express gcd of 2 numbers as an integer linear combination. |
| 104 |
function lincombgcd (a,b) |
| 105 |
local val1,val2 = a,b |
| 106 |
if type(a) ~= 'number' or type(b) ~= 'number' then |
| 107 |
error('Only numbers are expected.') |
| 108 |
return |
| 109 |
elseif a~= math.floor(a) or b~= math.floor(b) then |
| 110 |
error('Error: Only integers are expected.') |
| 111 |
return |
| 112 |
end |
| 113 |
|
| 114 |
local x,y=math.max(math.abs(a),math.abs(b)), |
| 115 |
math.min(math.abs(a),math.abs(b)) |
| 116 |
|
| 117 |
local a,b=math.max(math.abs(a),math.abs(b)), |
| 118 |
math.min(math.abs(a),math.abs(b)) |
| 119 |
|
| 120 |
if x == 0 and y == 0 then |
| 121 |
return |
| 122 |
("The gcd of $0$ and $0$ is clearly a linear combination of $0$ and $0$.") |
| 123 |
elseif y == 0 then |
| 124 |
return ("The gcd of " .. val1 .." and " .. val2 .. |
| 125 |
" is $" .. x .. "$" .. " and $" .. x .." = 1 " |
| 126 |
.."(".. x .. ")".." + 0(0)$.") |
| 127 |
end |
| 128 |
|
| 129 |
if x % y == 0 then |
| 130 |
return ("The gcd of " .. val1 .." and " .. val2 .. |
| 131 |
" is $" .. y .. "$" .. " and one number is a multiple of other.") |
| 132 |
end |
| 133 |
|
| 134 |
local e_1 = 1 |
| 135 |
local e_2 = 0 |
| 136 |
local e_3 = 0 |
| 137 |
local f_1 = 0 |
| 138 |
local f_2 = 1 |
| 139 |
local f_3 = 0 |
| 140 |
|
| 141 |
while (a > 0 and b > 0) do |
| 142 |
if (a > b) then |
| 143 |
q = a // b |
| 144 |
r = a % b |
| 145 |
|
| 146 |
if r > 0 then |
| 147 |
e_3 = e_1 - (q * e_2) |
| 148 |
e_1 = e_2 |
| 149 |
e_2 = e_3 |
| 150 |
f_3 = f_1 - (q * f_2) |
| 151 |
f_1 = f_2 |
| 152 |
f_2 = f_3 |
| 153 |
gcd = r |
| 154 |
end |
| 155 |
a = a % b |
| 156 |
else do |
| 157 |
q = b // a |
| 158 |
r = b % a |
| 159 |
if r > 0 then |
| 160 |
e_3 = e_1 - (q * e_2) |
| 161 |
e_1 = e_2 |
| 162 |
e_2 = e_3 |
| 163 |
f_3 = f_1 - (q * f_2) |
| 164 |
f_1 = f_2 |
| 165 |
f_2 = f_3 |
| 166 |
gcd = r |
| 167 |
end |
| 168 |
b = b % a |
| 169 |
end |
| 170 |
end |
| 171 |
end |
| 172 |
|
| 173 |
if math.abs(val1) >= math.abs(val2) then |
| 174 |
coeff1,coeff2 = val1, val2 |
| 175 |
if val1 < 0 and val2 < 0 then |
| 176 |
e_3,f_3 = -e_3,-f_3 |
| 177 |
elseif val1 > 0 and val2 < 0 then |
| 178 |
f_3 = -f_3 |
| 179 |
elseif val1 < 0 and val2 > 0 then |
| 180 |
e_3 = -e_3 |
| 181 |
end |
| 182 |
else |
| 183 |
coeff1,coeff2 = val2,val1 |
| 184 |
if val1 < 0 and val2 < 0 then |
| 185 |
e_3,f_3 = -e_3,-f_3 |
| 186 |
elseif val1 > 0 and val2 < 0 then |
| 187 |
e_3 = -e_3 |
| 188 |
elseif val1 < 0 and val2 > 0 then |
| 189 |
f_3 = -f_3 |
| 190 |
end |
| 191 |
end |
| 192 |
|
| 193 |
if coeff2 <0 then |
| 194 |
op = "" |
| 195 |
else |
| 196 |
op = " + " |
| 197 |
end |
| 198 |
|
| 199 |
return ("The gcd of " .. val1 .." and " .. val2 .. " is " .. gcd .. |
| 200 |
" and the equation $" .. coeff1 .."x" .. op .. coeff2 .."y = " |
| 201 |
..gcd .. "$ has a solution $(x,y) = (" .. e_3 .. "," .. f_3 ..")$.") |
| 202 |
end |
| 203 |
-- function to express gcd of 2 numbers as an integer linear combination with steps. |
| 204 |
function lincombgcdstepbystep (a,b) |
| 205 |
local val1,val2 = a,b |
| 206 |
if type(a) ~= 'number' or type(b) ~= 'number' then |
| 207 |
error('Only numbers are expected.') |
| 208 |
return |
| 209 |
elseif a~= math.floor(a) or b~= math.floor(b) then |
| 210 |
error('Error: Only integers are expected.') |
| 211 |
return |
| 212 |
end |
| 213 |
|
| 214 |
local x,y=math.max(math.abs(a),math.abs(b)), |
| 215 |
math.min(math.abs(a),math.abs(b)) |
| 216 |
|
| 217 |
local a,b=math.max(math.abs(a),math.abs(b)), |
| 218 |
math.min(math.abs(a),math.abs(b)) |
| 219 |
|
| 220 |
if x == 0 and y == 0 then |
| 221 |
return |
| 222 |
("The gcd of $0$ and $0$ is clearly a linear combination of $0$ and $0$.") |
| 223 |
elseif y == 0 then |
| 224 |
return ("The gcd of " .. val1 .." and " .. val2 .. |
| 225 |
" is $" .. x .. "$" .. " and $" .. x .." = 1 " |
| 226 |
.."(".. x .. ")".." + 0(0)$.") |
| 227 |
end |
| 228 |
|
| 229 |
if x % y == 0 then |
| 230 |
return ("The gcd of " .. val1 .." and " .. val2 .. |
| 231 |
" is $" .. y .. "$" .. " and one number is a multiple of other.") |
| 232 |
end |
| 233 |
|
| 234 |
local e_1 = 1 |
| 235 |
local e_2 = 0 |
| 236 |
local e_3 = 0 |
| 237 |
local f_1 = 0 |
| 238 |
local f_2 = 1 |
| 239 |
local f_3 = 0 |
| 240 |
local sep = "Step " |
| 241 |
local stcnt = 2 |
| 242 |
local cnt = 4 |
| 243 |
|
| 244 |
local tbl ={} |
| 245 |
tbl[1] = "Step 1:" .. x .. " is written as a linear combination of " |
| 246 |
.. x .. " and " .. y .. "." |
| 247 |
tbl[2] = "$".. x .. " = (" .. "1" .. ")" .. "(" .. x .. ") + " |
| 248 |
.. "(" .. "0" .. ")" .. "(" .. y .. ")$" |
| 249 |
|
| 250 |
tbl[3] = "Step 2:" .. y .. " is written as a linear combination of " |
| 251 |
.. x .. " and " .. y .. "." |
| 252 |
tbl[4] = "$".. y .. " = (" .. "0" .. ")".. "(" .. x .. ") + " |
| 253 |
.. "(" .. "1" .. ")" .. "(" .. y .. ")$" |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
while (a > 0 and b > 0) do |
| 258 |
if (a > b) then |
| 259 |
q = a // b |
| 260 |
r = a % b |
| 261 |
|
| 262 |
if r > 0 then |
| 263 |
e_3 = e_1 - (q * e_2) |
| 264 |
e_1 = e_2 |
| 265 |
e_2 = e_3 |
| 266 |
f_3 = f_1 - (q * f_2) |
| 267 |
f_1 = f_2 |
| 268 |
f_2 = f_3 |
| 269 |
|
| 270 |
cnt = cnt + 1 |
| 271 |
stcnt = stcnt + 1 |
| 272 |
|
| 273 |
tbl[cnt] = sep..stcnt ..": ".."The equation in Step " |
| 274 |
..(stcnt-1).. " is multiplied by " .. q .. |
| 275 |
" and subtracted from the equation in Step " |
| 276 |
..(stcnt-2) .. "." |
| 277 |
cnt = cnt +1 |
| 278 |
|
| 279 |
tbl[cnt] = "$".. r .. " = (" .. e_3 .. ")" .. "(" .. x .. ") + " |
| 280 |
.. "(" .. f_3 .. ")" .. "(" .. y .. ")$" |
| 281 |
gcd = r |
| 282 |
end |
| 283 |
a = a % b |
| 284 |
else do |
| 285 |
q = b // a |
| 286 |
r = b % a |
| 287 |
if r > 0 then |
| 288 |
e_3 = e_1 - (q * e_2) |
| 289 |
e_1 = e_2 |
| 290 |
e_2 = e_3 |
| 291 |
f_3 = f_1 - (q * f_2) |
| 292 |
f_1 = f_2 |
| 293 |
f_2 = f_3 |
| 294 |
cnt = cnt +1 |
| 295 |
stcnt = stcnt + 1 |
| 296 |
tbl[cnt] = sep..( stcnt) ..": ".."The equation in Step " |
| 297 |
..(stcnt-1).. " is multiplied by " .. q .. |
| 298 |
" and subtracted from the equation in Step " |
| 299 |
..(stcnt-2) .. "." |
| 300 |
cnt = cnt +1 |
| 301 |
tbl[cnt] = "$" .. r .. " = (" .. e_3 .. ")" .. "(" .. x .. ") + " |
| 302 |
.. "(" .. f_3 .. ")" .. "(" .. y .. ")$" |
| 303 |
gcd = r |
| 304 |
end |
| 305 |
b = b % a |
| 306 |
end |
| 307 |
end |
| 308 |
end |
| 309 |
|
| 310 |
if math.abs(val1) >= math.abs(val2) then |
| 311 |
coeff1,coeff2 = val1, val2 |
| 312 |
if val1 < 0 and val2 < 0 then |
| 313 |
e_3,f_3 = -e_3,-f_3 |
| 314 |
elseif val1 > 0 and val2 < 0 then |
| 315 |
f_3 = -f_3 |
| 316 |
elseif val1 < 0 and val2 > 0 then |
| 317 |
e_3 = -e_3 |
| 318 |
end |
| 319 |
else |
| 320 |
coeff1,coeff2 = val2,val1 |
| 321 |
if val1 < 0 and val2 < 0 then |
| 322 |
e_3,f_3 = -e_3,-f_3 |
| 323 |
elseif val1 > 0 and val2 < 0 then |
| 324 |
e_3 = -e_3 |
| 325 |
elseif val1 < 0 and val2 > 0 then |
| 326 |
f_3 = -f_3 |
| 327 |
end |
| 328 |
end |
| 329 |
|
| 330 |
if coeff2 <0 then |
| 331 |
op = "" |
| 332 |
else |
| 333 |
op = " + " |
| 334 |
end |
| 335 |
|
| 336 |
tbl[cnt+1] = "The gcd of " .. val1 .." and " .. val2 .. " is " .. gcd .. |
| 337 |
" and the equation $" .. coeff1 .."x" .. op .. coeff2 .."y = " |
| 338 |
..gcd .. "$ has a solution $(x,y) = (" .. e_3 .. "," .. f_3 ..")$." |
| 339 |
return table.concat(tbl,"\\\\") |
| 340 |
|
| 341 |
end |
| 342 |
|
| 343 |
\end{luacode*} |
| 344 |
|
| 345 |
\newcommand\luagcd[1]{\directlua{tex.sprint(luagcd(#1))}} |
| 346 |
|
| 347 |
\newcommand\luagcdwithsteps[2] |
| 348 |
{\directlua{tex.sprint(stepbystepgcd(#1,#2))}} |
| 349 |
|
| 350 |
\newcommand\luagcdlincomb[2] |
| 351 |
{\directlua{tex.sprint(lincombgcd(#1,#2))}} |
| 352 |
|
| 353 |
\newcommand\luagcdlincombwithsteps[2] |
| 354 |
{\directlua{tex.sprint(lincombgcdstepbystep(#1,#2))}} |
| 355 |
|
| 356 |
\endinput |