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