| 1 |
--- @class Integer |
| 2 |
--- Represents an element of the ring of integers. |
| 3 |
--- @field self table<number, number> |
| 4 |
--- @field sign number |
| 5 |
Integer = {} |
| 6 |
local __Integer = {} |
| 7 |
|
| 8 |
-------------------------- |
| 9 |
-- Static functionality -- |
| 10 |
-------------------------- |
| 11 |
|
| 12 |
-- The length of each digit in base 10. 10^15 < 2^53 < 10^16, so 15 is the highest value that will work with double-percision numbers. |
| 13 |
-- For multiplication to work properly, however, this also must be even so we can take the square root of the digit size exactly. |
| 14 |
-- 10^14 is still larger than 2^26, so it is still efficient to do multiplication this way. |
| 15 |
Integer.DIGITLENGTH = 14 |
| 16 |
-- The maximum size for a digit. While this doesn't need to be a power of 10, it makes implementing converting to and from strings much easier. |
| 17 |
Integer.DIGITSIZE = 10 ^ Integer.DIGITLENGTH |
| 18 |
-- Partition size for multiplying integers so we can get both the upper and lower bits of each digits |
| 19 |
Integer.PARTITIONSIZE = math.floor(math.sqrt(Integer.DIGITSIZE)) |
| 20 |
|
| 21 |
--- Method for computing the gcd of two integers using Euclid's algorithm. |
| 22 |
--- @param a Integer |
| 23 |
--- @param b Integer |
| 24 |
--- @return Integer |
| 25 |
function Integer.gcd(a, b) |
| 26 |
while b ~= Integer.zero() do |
| 27 |
a, b = b, a%b |
| 28 |
end |
| 29 |
return a |
| 30 |
end |
| 31 |
|
| 32 |
--- Method for computing the gcd of two integers using Euclid's algorithm. |
| 33 |
--- Also returns Bezout's coefficients via extended gcd. |
| 34 |
--- @param a Integer |
| 35 |
--- @param b Integer |
| 36 |
--- @return Integer, Integer, Integer |
| 37 |
function Integer.extendedgcd(a, b) |
| 38 |
local oldr, r = a, b |
| 39 |
local olds, s = Integer.one(), Integer.zero() |
| 40 |
local oldt, t = Integer.zero(), Integer.one() |
| 41 |
while r ~= Integer.zero() do |
| 42 |
local q = oldr // r |
| 43 |
oldr, r = r, oldr - q*r |
| 44 |
olds, s = s, olds - q*s |
| 45 |
oldt, t = t, oldt - q*t |
| 46 |
end |
| 47 |
return oldr, olds, oldt |
| 48 |
end |
| 49 |
|
| 50 |
--- Method for computing the larger of two integers. |
| 51 |
--- Also returns the other integer for sorting purposes. |
| 52 |
--- @param a Integer |
| 53 |
--- @param b Integer |
| 54 |
--- @return Integer, Integer |
| 55 |
function Integer.max(a, b) |
| 56 |
if a > b then |
| 57 |
return a, b |
| 58 |
end |
| 59 |
return b, a |
| 60 |
end |
| 61 |
|
| 62 |
--- Method for computing the smaller of two integers. |
| 63 |
--- Also returns the other integer for sorting purposes. |
| 64 |
--- @param a Integer |
| 65 |
--- @param b Integer |
| 66 |
--- @return Integer, Integer |
| 67 |
function Integer.min(a, b) |
| 68 |
if a < b then |
| 69 |
return a, b |
| 70 |
end |
| 71 |
return b, a |
| 72 |
end |
| 73 |
|
| 74 |
--- Methods for computing the larger magnitude of two integers. |
| 75 |
--- Also returns the other integer for sorting purposes, and the number -1 if the two values were swapped, 1 if not. |
| 76 |
--- @param a Integer |
| 77 |
--- @param b Integer |
| 78 |
--- @return Integer, Integer, number |
| 79 |
function Integer.absmax(a, b) |
| 80 |
if b:ltabs(a) then |
| 81 |
return a, b, 1 |
| 82 |
end |
| 83 |
return b, a, -1 |
| 84 |
end |
| 85 |
|
| 86 |
-- Returns the ceiling of the log base (defaults to 10) of a. |
| 87 |
-- In other words, returns the least n such that base^n > a. |
| 88 |
--- @param a Integer |
| 89 |
--- @param base Integer |
| 90 |
--- @return Integer |
| 91 |
function Integer.ceillog(a, base) |
| 92 |
base = base or Integer(10) |
| 93 |
if a == Integer.one() then |
| 94 |
return Integer.one() |
| 95 |
end |
| 96 |
local k = Integer.zero() |
| 97 |
|
| 98 |
while (base ^ k) < a do |
| 99 |
k = k + Integer.one() |
| 100 |
end |
| 101 |
|
| 102 |
return k |
| 103 |
end |
| 104 |
|
| 105 |
--- Returns a ^ b (mod n). This should be used when a ^ b is potentially large. |
| 106 |
--- @param a Integer |
| 107 |
--- @param b Integer |
| 108 |
--- @param n Integer |
| 109 |
--- @return Integer |
| 110 |
function Integer.powmod(a, b, n) |
| 111 |
if n == Integer.one() then |
| 112 |
return Integer.zero() |
| 113 |
else |
| 114 |
local r = Integer.one() |
| 115 |
a = a % n |
| 116 |
while b > Integer.zero() do |
| 117 |
if b % Integer(2) == Integer.one() then |
| 118 |
r = (r * a) % n |
| 119 |
end |
| 120 |
a = (a ^ Integer(2)) % n |
| 121 |
b = b // Integer(2) |
| 122 |
end |
| 123 |
return r |
| 124 |
end |
| 125 |
end |
| 126 |
|
| 127 |
--- @return RingIdentifier |
| 128 |
local t = {ring=Integer} |
| 129 |
t = setmetatable(t, {__index = Integer, __eq = function(a, b) |
| 130 |
return a["ring"] == b["ring"] |
| 131 |
end, __tostring = function(a) |
| 132 |
return "ZZ" |
| 133 |
end}) |
| 134 |
function Integer.makering() |
| 135 |
return t |
| 136 |
end |
| 137 |
|
| 138 |
|
| 139 |
---------------------------- |
| 140 |
-- Instance functionality -- |
| 141 |
---------------------------- |
| 142 |
|
| 143 |
-- So we don't have to copy the Euclidean operations each time we create an integer. |
| 144 |
__IntegerOperations = Copy(__EuclideanOperations) |
| 145 |
__IntegerOperations.__index = Integer |
| 146 |
__IntegerOperations.__tostring = function(a) -- Only works if the digit size is a power of 10 |
| 147 |
local out = "" |
| 148 |
for i, digit in ipairs(a) do |
| 149 |
local pre = tostring(math.floor(digit)) |
| 150 |
if i ~= #a then |
| 151 |
while #pre ~= Integer.DIGITLENGTH do |
| 152 |
pre = "0" .. pre |
| 153 |
end |
| 154 |
end |
| 155 |
out = pre .. out |
| 156 |
end |
| 157 |
if a.sign == -1 then |
| 158 |
out = "-" .. out |
| 159 |
end |
| 160 |
return out |
| 161 |
end |
| 162 |
__IntegerOperations.__div = function(a, b) -- Constructor for a rational number disguised as division |
| 163 |
if not b.getring then |
| 164 |
return BinaryOperation.DIVEXP({a, b}) |
| 165 |
end |
| 166 |
if(a:getring() == Integer:getring() and b:getring() == Integer:getring()) then |
| 167 |
return Rational(a, b) |
| 168 |
end |
| 169 |
return __FieldOperations.__div(a, b) |
| 170 |
end |
| 171 |
__IntegerOperations.__concat = function(a, b) -- Like a decimal, but fancier. Used mainly for the parser with decimal numbers. |
| 172 |
return a + b / (Integer(10) ^ Integer.ceillog(b)) |
| 173 |
end |
| 174 |
|
| 175 |
--- Creates a new integer given a string or number representation of the integer. |
| 176 |
--- @param n number|string|Integer |
| 177 |
--- @return Integer |
| 178 |
function Integer:new(n) |
| 179 |
local o = {} |
| 180 |
o = setmetatable(o, __IntegerOperations) |
| 181 |
|
| 182 |
if not n then |
| 183 |
o[1] = 0 |
| 184 |
o.sign = 0 |
| 185 |
return o |
| 186 |
end |
| 187 |
|
| 188 |
-- Can convert any floating-point number into an integer, though we generally only want to pass whole numbers into this. |
| 189 |
-- This will only approximate very large floating point numbers to a small proportion of the total significant digits |
| 190 |
-- After that the result will just be nonsense - strings should probably be used for big numbers |
| 191 |
if type(n) == "number" then |
| 192 |
n = math.floor(n) |
| 193 |
if n == 0 then |
| 194 |
o[1] = 0 |
| 195 |
o.sign = 0 |
| 196 |
else |
| 197 |
if n < 0 then |
| 198 |
n = -n |
| 199 |
o.sign = -1 |
| 200 |
else |
| 201 |
o.sign = 1 |
| 202 |
end |
| 203 |
local i = 1 |
| 204 |
while n >= Integer.DIGITSIZE do |
| 205 |
o[i] = n % Integer.DIGITSIZE |
| 206 |
n = n // Integer.DIGITSIZE |
| 207 |
i = i + 1 |
| 208 |
end |
| 209 |
o[i] = n |
| 210 |
end |
| 211 |
-- Only works on strings that are exact (signed) integers |
| 212 |
elseif type(n) == "string" then |
| 213 |
if not tonumber(n) then |
| 214 |
error("Sent parameter of wrong type: " .. n .. " is not an integer.") |
| 215 |
end |
| 216 |
if n == "0" then |
| 217 |
o[1] = 0 |
| 218 |
o.sign = 0 |
| 219 |
else |
| 220 |
local s = 1 |
| 221 |
if string.sub(n, 1, 1) == "-" then |
| 222 |
s = s + 1 |
| 223 |
o.sign = -1 |
| 224 |
else |
| 225 |
o.sign = 1 |
| 226 |
end |
| 227 |
|
| 228 |
while string.sub(n, s, s) == "0" do |
| 229 |
s = s + 1 |
| 230 |
end |
| 231 |
|
| 232 |
local e = #n |
| 233 |
local i = 1 |
| 234 |
while e > s + Integer.DIGITLENGTH - 1 do |
| 235 |
o[i] = tonumber(string.sub(n, e - Integer.DIGITLENGTH + 1, e)) |
| 236 |
e = e - Integer.DIGITLENGTH |
| 237 |
i = i + 1 |
| 238 |
end |
| 239 |
o[i] = tonumber(string.sub(n, s, e)) or 0 |
| 240 |
end |
| 241 |
-- Copying is expensive in Lua, so this constructor probably should only sparsely be called with an Integer argument. |
| 242 |
elseif type(n) == "table" then |
| 243 |
o = Copy(n) |
| 244 |
else |
| 245 |
error("Sent parameter of wrong type: Integer does not accept " .. type(n) .. ".") |
| 246 |
end |
| 247 |
|
| 248 |
return o |
| 249 |
end |
| 250 |
|
| 251 |
--- Returns the ring this object is an element of. |
| 252 |
--- @return RingIdentifier |
| 253 |
function Integer:getring() |
| 254 |
return t |
| 255 |
end |
| 256 |
|
| 257 |
--- @param ring RingIdentifier |
| 258 |
--- @return Ring |
| 259 |
function Integer:inring(ring) |
| 260 |
if ring == self:getring() then |
| 261 |
return self |
| 262 |
end |
| 263 |
|
| 264 |
if ring == PolynomialRing:getring() then |
| 265 |
return PolynomialRing({self:inring(ring.child)}, ring.symbol) |
| 266 |
end |
| 267 |
|
| 268 |
if ring == Rational:getring() then |
| 269 |
if ring.child then |
| 270 |
return Rational(self:inring(ring.child), self:inring(ring.child):one(), true) |
| 271 |
end |
| 272 |
return Rational(self, Integer.one(), true):inring(ring) |
| 273 |
end |
| 274 |
|
| 275 |
if ring == IntegerModN:getring() then |
| 276 |
return IntegerModN(self, ring.modulus) |
| 277 |
end |
| 278 |
|
| 279 |
error("Unable to convert element to proper ring.") |
| 280 |
end |
| 281 |
|
| 282 |
--- @param b Integer |
| 283 |
--- @return Integer |
| 284 |
function Integer:add(b) |
| 285 |
if self.sign == 1 and b.sign == -1 then |
| 286 |
return self:usub(b, 1) |
| 287 |
end |
| 288 |
if self.sign == -1 and b.sign == 1 then |
| 289 |
return self:usub(b, -1) |
| 290 |
end |
| 291 |
|
| 292 |
local sign = self.sign |
| 293 |
if sign == 0 then |
| 294 |
sign = b.sign |
| 295 |
end |
| 296 |
return self:uadd(b, sign) |
| 297 |
end |
| 298 |
|
| 299 |
--- Addition without sign so we don't have to create an entire new integer when switching signs. |
| 300 |
--- @param b Integer |
| 301 |
--- @param sign number |
| 302 |
--- @return Integer |
| 303 |
function Integer:uadd(b, sign) |
| 304 |
local o = Integer() |
| 305 |
o.sign = sign |
| 306 |
|
| 307 |
local c = 0 |
| 308 |
local n = math.max(#self, #b) |
| 309 |
for i = 1, n do |
| 310 |
local s = (self[i] or 0) + (b[i] or 0) + c |
| 311 |
if s >= Integer.DIGITSIZE then |
| 312 |
o[i] = s - Integer.DIGITSIZE |
| 313 |
c = 1 |
| 314 |
else |
| 315 |
o[i] = s |
| 316 |
c = 0 |
| 317 |
end |
| 318 |
end |
| 319 |
if c == 1 then |
| 320 |
o[n + 1] = c |
| 321 |
end |
| 322 |
return o |
| 323 |
end |
| 324 |
|
| 325 |
--- @param b Integer |
| 326 |
--- @return Integer |
| 327 |
function Integer:sub(b) |
| 328 |
if self.sign == 1 and b.sign == -1 then |
| 329 |
return self:uadd(b, 1) |
| 330 |
end |
| 331 |
if self.sign == -1 and b.sign == 1 then |
| 332 |
return self:uadd(b, -1) |
| 333 |
end |
| 334 |
|
| 335 |
local sign = self.sign |
| 336 |
if sign == 0 then |
| 337 |
sign = b.sign |
| 338 |
end |
| 339 |
return self:usub(b, sign) |
| 340 |
end |
| 341 |
|
| 342 |
-- Subtraction without sign so we don't have to create an entire new integer when switching signs. |
| 343 |
-- Uses subtraction by compliments. |
| 344 |
--- @param b Integer |
| 345 |
--- @param sign number |
| 346 |
--- @return Integer |
| 347 |
function Integer:usub(b, sign) |
| 348 |
local a, b, swap = Integer.absmax(self, b) |
| 349 |
local o = Integer() |
| 350 |
o.sign = sign * swap |
| 351 |
|
| 352 |
local c = 0 |
| 353 |
local n = #a |
| 354 |
for i = 1, n do |
| 355 |
local s = (a[i] or 0) + Integer.DIGITSIZE - 1 - (b[i] or 0) + c |
| 356 |
if i == 1 then |
| 357 |
s = s + 1 |
| 358 |
end |
| 359 |
if s >= Integer.DIGITSIZE then |
| 360 |
o[i] = s - Integer.DIGITSIZE |
| 361 |
c = 1 |
| 362 |
else |
| 363 |
o[i] = s |
| 364 |
c = 0 |
| 365 |
end |
| 366 |
end |
| 367 |
|
| 368 |
-- Remove leading zero digits, since we want integer representations to be unique. |
| 369 |
while o[n] == 0 do |
| 370 |
o[n] = nil |
| 371 |
n = n - 1 |
| 372 |
end |
| 373 |
|
| 374 |
if not o[1] then |
| 375 |
o[1] = 0 |
| 376 |
o.sign = 0 |
| 377 |
end |
| 378 |
|
| 379 |
return o |
| 380 |
end |
| 381 |
|
| 382 |
--- @return Integer |
| 383 |
function Integer:neg() |
| 384 |
local o = Integer() |
| 385 |
o.sign = -self.sign |
| 386 |
for i, digit in ipairs(self) do |
| 387 |
o[i] = digit |
| 388 |
end |
| 389 |
return o |
| 390 |
end |
| 391 |
|
| 392 |
--- @param b Integer |
| 393 |
--- @return Integer |
| 394 |
function Integer:mul(b) |
| 395 |
local o = Integer() |
| 396 |
o.sign = self.sign * b.sign |
| 397 |
if o.sign == 0 then |
| 398 |
o[1] = 0 |
| 399 |
return o |
| 400 |
end |
| 401 |
|
| 402 |
-- Fast single-digit multiplication in the most common case |
| 403 |
if #self == 1 and #b == 1 then |
| 404 |
o[2], o[1] = self:mulone(self[1], b[1]) |
| 405 |
|
| 406 |
if o[2] == 0 then |
| 407 |
o[2] = nil |
| 408 |
end |
| 409 |
|
| 410 |
return o |
| 411 |
end |
| 412 |
|
| 413 |
-- "Grade school" multiplication algorithm for numbers with small numbers of digits works faster than Karatsuba |
| 414 |
local n = #self |
| 415 |
local m = #b |
| 416 |
o[1] = 0 |
| 417 |
o[2] = 0 |
| 418 |
for i = 2, n+m do |
| 419 |
o[i + 1] = 0 |
| 420 |
for j = math.max(1, i-m), math.min(n, i-1) do |
| 421 |
local u, l = self:mulone(self[j], b[i - j]) |
| 422 |
o[i - 1] = o[i - 1] + l |
| 423 |
o[i] = o[i] + u |
| 424 |
if o[i - 1] >= Integer.DIGITSIZE then |
| 425 |
o[i - 1] = o[i - 1] - Integer.DIGITSIZE |
| 426 |
o[i] = o[i] + 1 |
| 427 |
end |
| 428 |
if o[i] >= Integer.DIGITSIZE then |
| 429 |
o[i] = o[i] - Integer.DIGITSIZE |
| 430 |
o[i + 1] = o[i + 1] + 1 |
| 431 |
end |
| 432 |
end |
| 433 |
end |
| 434 |
|
| 435 |
-- Remove leading zero digits, since we want integer representations to be unique. |
| 436 |
if o[n+m+1] == 0 then |
| 437 |
o[n+m+1] = nil |
| 438 |
end |
| 439 |
|
| 440 |
if o[n+m] == 0 then |
| 441 |
o[n+m] = nil |
| 442 |
end |
| 443 |
|
| 444 |
return o |
| 445 |
end |
| 446 |
|
| 447 |
--- Multiplies two single-digit numbers and returns two digits. |
| 448 |
--- @param a number |
| 449 |
--- @param b number |
| 450 |
--- @return number, number |
| 451 |
function Integer:mulone(a, b) |
| 452 |
local P = Integer.PARTITIONSIZE |
| 453 |
|
| 454 |
local a1 = a // P |
| 455 |
local a2 = a % P |
| 456 |
local b1 = b // P |
| 457 |
local b2 = b % P |
| 458 |
|
| 459 |
local u = a1 * b1 |
| 460 |
local l = a2 * b2 |
| 461 |
|
| 462 |
local m = ((a1 * b2) + (b1 * a2)) |
| 463 |
local mu = m // P |
| 464 |
local ml = m % P |
| 465 |
|
| 466 |
u = u + mu |
| 467 |
l = l + ml * P |
| 468 |
|
| 469 |
if l >= Integer.DIGITSIZE then |
| 470 |
l = l - Integer.DIGITSIZE |
| 471 |
u = u + 1 |
| 472 |
end |
| 473 |
|
| 474 |
return u, l |
| 475 |
end |
| 476 |
|
| 477 |
--- Naive exponentiation is slow even for small exponents, so this uses binary exponentiation. |
| 478 |
--- @param b Integer |
| 479 |
--- @return Integer |
| 480 |
function Integer:pow(b) |
| 481 |
if b < Integer.zero() then |
| 482 |
return Integer.one() / (self ^ -b) |
| 483 |
end |
| 484 |
|
| 485 |
if b == Integer.zero() then |
| 486 |
return Integer.one() |
| 487 |
end |
| 488 |
|
| 489 |
-- Fast single-digit exponentiation |
| 490 |
if #self == 1 and #b == 1 then |
| 491 |
local test = (self.sign * self[1]) ^ b[1] |
| 492 |
if test < Integer.DIGITSIZE and test > -Integer.DIGITSIZE then |
| 493 |
return Integer(test) |
| 494 |
end |
| 495 |
end |
| 496 |
|
| 497 |
local x = self |
| 498 |
local y = Integer.one() |
| 499 |
while b > Integer.one() do |
| 500 |
if b[1] % 2 == 0 then |
| 501 |
x = x * x |
| 502 |
b = b:divbytwo() |
| 503 |
else |
| 504 |
y = x * y |
| 505 |
x = x * x |
| 506 |
b = b:divbytwo() |
| 507 |
end |
| 508 |
end |
| 509 |
|
| 510 |
return x * y |
| 511 |
end |
| 512 |
|
| 513 |
-- Fast integer division by two for binary exponentiation. |
| 514 |
--- @return Integer |
| 515 |
function Integer:divbytwo() |
| 516 |
local o = Integer() |
| 517 |
o.sign = self.sign |
| 518 |
for i = #self, 1, -1 do |
| 519 |
if self[i] % 2 == 0 then |
| 520 |
o[i] = self[i] // 2 |
| 521 |
else |
| 522 |
o[i] = self[i] // 2 |
| 523 |
if i ~= 1 then |
| 524 |
o[i - 1] = self[i - 1] * 2 |
| 525 |
end |
| 526 |
end |
| 527 |
end |
| 528 |
return o |
| 529 |
end |
| 530 |
|
| 531 |
--- Division with remainder over the integers. Uses the standard base 10 long division algorithm. |
| 532 |
--- @param b Integer |
| 533 |
--- @return Integer, Integer |
| 534 |
function Integer:divremainder(b) |
| 535 |
if self >= Integer.zero() and b > self or self <= Integer.zero() and b < self then |
| 536 |
return Integer.zero(), Integer(self) |
| 537 |
end |
| 538 |
|
| 539 |
if #self == 1 and #b == 1 then |
| 540 |
return Integer((self[1]*self.sign) // (b[1]*b.sign)), Integer((self[1]*self.sign) % (b[1]*b.sign)) |
| 541 |
end |
| 542 |
|
| 543 |
local Q = Integer() |
| 544 |
local R = Integer() |
| 545 |
|
| 546 |
Q.sign = self.sign * b.sign |
| 547 |
R.sign = 1 |
| 548 |
local negativemod = false |
| 549 |
if b.sign == -1 then |
| 550 |
b.sign = -b.sign |
| 551 |
negativemod = true |
| 552 |
end |
| 553 |
|
| 554 |
for i = #self, 1, -1 do |
| 555 |
local s = tostring(math.floor(self[i])) |
| 556 |
while i ~= #self and #s ~= Integer.DIGITLENGTH do |
| 557 |
s = "0" .. s |
| 558 |
end |
| 559 |
Q[i] = 0 |
| 560 |
for j = 1, #s do |
| 561 |
R = R:mulbyten() |
| 562 |
R[1] = R[1] + tonumber(string.sub(s, j, j)) |
| 563 |
if R[1] > 0 then |
| 564 |
R.sign = 1 |
| 565 |
end |
| 566 |
while R >= b do |
| 567 |
R = R - b |
| 568 |
Q[i] = Q[i] + 10^(#s - j) |
| 569 |
end |
| 570 |
end |
| 571 |
end |
| 572 |
|
| 573 |
-- Remove leading zero digits, since we want integer representations to be unique. |
| 574 |
while Q[#Q] == 0 do |
| 575 |
Q[#Q] = nil |
| 576 |
end |
| 577 |
|
| 578 |
if negativemod then |
| 579 |
R = -R |
| 580 |
b.sign = -b.sign |
| 581 |
elseif self.sign == -1 then |
| 582 |
R = b - R |
| 583 |
end |
| 584 |
|
| 585 |
return Q, R |
| 586 |
end |
| 587 |
|
| 588 |
--- Fast in-place multiplication by ten for the division algorithm. This means the number IS MODIFIED by this method unlike the rest of the library. |
| 589 |
--- @return Integer |
| 590 |
function Integer:mulbyten() |
| 591 |
local DIGITSIZE = Integer.DIGITSIZE |
| 592 |
for i, _ in ipairs(self) do |
| 593 |
self[i] = self[i] * 10 |
| 594 |
end |
| 595 |
for i, _ in ipairs(self) do |
| 596 |
if self[i] > DIGITSIZE then |
| 597 |
local msd = self[i] // DIGITSIZE |
| 598 |
if self[i+1] then |
| 599 |
self[i+1] = self[i+1] + msd |
| 600 |
else |
| 601 |
self[i+1] = msd |
| 602 |
end |
| 603 |
self[i] = self[i] - DIGITSIZE*msd |
| 604 |
end |
| 605 |
end |
| 606 |
return self |
| 607 |
end |
| 608 |
|
| 609 |
--- @param b Integer |
| 610 |
--- @return boolean |
| 611 |
function Integer:eq(b) |
| 612 |
for i, digit in ipairs(self) do |
| 613 |
if not b[i] or not (b[i] == digit) then |
| 614 |
return false |
| 615 |
end |
| 616 |
end |
| 617 |
return #self == #b and self.sign == b.sign |
| 618 |
end |
| 619 |
|
| 620 |
--- @param b Integer |
| 621 |
--- @return boolean |
| 622 |
function Integer:lt(b) |
| 623 |
local selfsize = #self |
| 624 |
local bsize = #b |
| 625 |
if selfsize < bsize then |
| 626 |
return b.sign == 1 |
| 627 |
end |
| 628 |
if selfsize > bsize then |
| 629 |
return self.sign == -1 |
| 630 |
end |
| 631 |
local n = selfsize |
| 632 |
while n > 0 do |
| 633 |
if self[n]*self.sign < b[n]*b.sign then |
| 634 |
return true |
| 635 |
end |
| 636 |
if self[n]*self.sign > b[n]*b.sign then |
| 637 |
return false |
| 638 |
end |
| 639 |
n = n - 1 |
| 640 |
end |
| 641 |
return false |
| 642 |
end |
| 643 |
|
| 644 |
--- Same as less than, but ignores signs. |
| 645 |
--- @param b Integer |
| 646 |
--- @return boolean |
| 647 |
function Integer:ltabs(b) |
| 648 |
if #self < #b then |
| 649 |
return true |
| 650 |
end |
| 651 |
if #self > #b then |
| 652 |
return false |
| 653 |
end |
| 654 |
local n = #self |
| 655 |
while n > 0 do |
| 656 |
if self[n] < b[n] then |
| 657 |
return true |
| 658 |
end |
| 659 |
if self[n] > b[n] then |
| 660 |
return false |
| 661 |
end |
| 662 |
n = n - 1 |
| 663 |
end |
| 664 |
return false |
| 665 |
end |
| 666 |
|
| 667 |
--- @param b Integer |
| 668 |
--- @return boolean |
| 669 |
function Integer:le(b) |
| 670 |
local selfsize = #self |
| 671 |
local bsize = #b |
| 672 |
if selfsize < bsize then |
| 673 |
return b.sign == 1 |
| 674 |
end |
| 675 |
if selfsize > bsize then |
| 676 |
return self.sign == -1 |
| 677 |
end |
| 678 |
local n = selfsize |
| 679 |
while n > 0 do |
| 680 |
if self[n]*self.sign < b[n]*b.sign then |
| 681 |
return true |
| 682 |
end |
| 683 |
if self[n]*self.sign > b[n]*b.sign then |
| 684 |
return false |
| 685 |
end |
| 686 |
n = n - 1 |
| 687 |
end |
| 688 |
return true |
| 689 |
end |
| 690 |
|
| 691 |
local zero = Integer:new(0) |
| 692 |
--- @return Integer |
| 693 |
function Integer:zero() |
| 694 |
return zero |
| 695 |
end |
| 696 |
|
| 697 |
local one = Integer:new(1) |
| 698 |
--- @return Integer |
| 699 |
function Integer:one() |
| 700 |
return one |
| 701 |
end |
| 702 |
|
| 703 |
--- Returns this integer as a floating point number. Can only approximate the value of large integers. |
| 704 |
--- @return number |
| 705 |
function Integer:asnumber() |
| 706 |
local n = 0 |
| 707 |
for i, digit in ipairs(self) do |
| 708 |
n = n + digit * Integer.DIGITSIZE ^ (i - 1) |
| 709 |
end |
| 710 |
return self.sign*math.floor(n) |
| 711 |
end |
| 712 |
|
| 713 |
--- Returns all positive divisors of the integer. Not guarenteed to be in any order. |
| 714 |
--- @return table<number, Integer> |
| 715 |
function Integer:divisors() |
| 716 |
local primefactors = self:primefactorizationrec() |
| 717 |
local divisors = {} |
| 718 |
|
| 719 |
local terms = {} |
| 720 |
for prime in pairs(primefactors) do |
| 721 |
if prime == Integer(-1) then |
| 722 |
primefactors[prime] = nil |
| 723 |
end |
| 724 |
terms[prime] = Integer.zero() |
| 725 |
end |
| 726 |
|
| 727 |
local divisor = Integer.one() |
| 728 |
|
| 729 |
while true do |
| 730 |
divisors[#divisors+1] = divisor |
| 731 |
for prime, power in pairs(primefactors) do |
| 732 |
if terms[prime] < power then |
| 733 |
terms[prime] = terms[prime] + Integer.one() |
| 734 |
divisor = divisor * prime |
| 735 |
break |
| 736 |
else |
| 737 |
terms[prime] = Integer.zero() |
| 738 |
divisor = divisor / (prime ^ power) |
| 739 |
end |
| 740 |
end |
| 741 |
if divisor == Integer.one() then |
| 742 |
break |
| 743 |
end |
| 744 |
end |
| 745 |
|
| 746 |
return divisors |
| 747 |
end |
| 748 |
|
| 749 |
--- Returns whether this integer is a prime power, of the form p^a for prime p and positive integer a. |
| 750 |
--- If it is a prime power, also returns the prime and the power. |
| 751 |
--- @return boolean, Expression|nil, Expression|nil |
| 752 |
function Integer:isprimepower() |
| 753 |
if self <= Integer.one() then |
| 754 |
return false |
| 755 |
end |
| 756 |
local factorization = self:primefactorization() |
| 757 |
if factorization:type() == BinaryOperation and #factorization:subexpressions() == 1 then |
| 758 |
return true, factorization.expressions[1].expressions[2], factorization.expressions[1].expressions[1] |
| 759 |
end |
| 760 |
return false |
| 761 |
end |
| 762 |
|
| 763 |
--- Returns whether this integer is a perfect power, of the form a^b for positive integers a and b. |
| 764 |
--- If it is a prime power, also returns the prime and the power. |
| 765 |
--- @return boolean, Expression|nil, Expression|nil |
| 766 |
function Integer:isperfectpower() |
| 767 |
if self <= Integer.one() then |
| 768 |
return false |
| 769 |
end |
| 770 |
local factorization = self:primefactorization() |
| 771 |
if factorization:type() ~= BinaryOperation then |
| 772 |
return false |
| 773 |
end |
| 774 |
local power = Integer.zero() |
| 775 |
for _, term in ipairs(factorization:subexpressions()) do |
| 776 |
power = Integer.gcd(power, term.expressions[2]) |
| 777 |
if power == Integer.one() then |
| 778 |
return false |
| 779 |
end |
| 780 |
end |
| 781 |
local base = Integer.one() |
| 782 |
for _, term in ipairs(factorization:subexpressions()) do |
| 783 |
base = base * term.expressions[1] ^ (term.expressions[2] / power) |
| 784 |
end |
| 785 |
return true, base, power |
| 786 |
end |
| 787 |
|
| 788 |
--- Returns the prime factorization of this integer as a expression. |
| 789 |
--- @return Expression |
| 790 |
function Integer:primefactorization() |
| 791 |
if not Integer.FACTORIZATIONLIMIT then |
| 792 |
Integer.FACTORIZATIONLIMIT = Integer(Integer.DIGITSIZE) |
| 793 |
end |
| 794 |
if self > Integer.FACTORIZATIONLIMIT then |
| 795 |
return self |
| 796 |
end |
| 797 |
local result = self:primefactorizationrec() |
| 798 |
local mul = {} |
| 799 |
local i = 1 |
| 800 |
for factor, degree in pairs(result) do |
| 801 |
mul[i] = BinaryOperation.POWEXP({factor, degree}) |
| 802 |
i = i + 1 |
| 803 |
end |
| 804 |
return BinaryOperation.MULEXP(mul):lock(Expression.NIL) |
| 805 |
end |
| 806 |
|
| 807 |
--- Recursive part of prime factorization using Pollard Rho. |
| 808 |
function Integer:primefactorizationrec() |
| 809 |
if self < Integer.zero() then |
| 810 |
return Integer.mergefactors({[Integer(-1)]=Integer.one()}, (-self):primefactorizationrec()) |
| 811 |
end |
| 812 |
if self == Integer.one() then |
| 813 |
return {[Integer.one()]=Integer.one()} |
| 814 |
end |
| 815 |
local result = self:findafactor() |
| 816 |
if result == self then |
| 817 |
return {[result]=Integer.one()} |
| 818 |
end |
| 819 |
local remaining = self / result |
| 820 |
return Integer.mergefactors(result:primefactorizationrec(), remaining:primefactorizationrec()) |
| 821 |
end |
| 822 |
|
| 823 |
|
| 824 |
function Integer.mergefactors(a, b) |
| 825 |
local result = Copy(a) |
| 826 |
|
| 827 |
for factor, degree in pairs(b) do |
| 828 |
for ofactor, odegree in pairs(result) do |
| 829 |
if factor == ofactor then |
| 830 |
result[ofactor] = degree + odegree |
| 831 |
goto continue |
| 832 |
end |
| 833 |
end |
| 834 |
result[factor] = degree |
| 835 |
::continue:: |
| 836 |
end |
| 837 |
return result |
| 838 |
end |
| 839 |
|
| 840 |
-- Return a non-trivial factor of n via Pollard Rho, or returns n if n is prime. |
| 841 |
function Integer:findafactor() |
| 842 |
if self:isprime() then |
| 843 |
return self |
| 844 |
end |
| 845 |
|
| 846 |
if self % Integer(2) == Integer.zero() then |
| 847 |
return Integer(2) |
| 848 |
end |
| 849 |
|
| 850 |
if self % Integer(3) == Integer.zero() then |
| 851 |
return Integer(3) |
| 852 |
end |
| 853 |
|
| 854 |
if self % Integer(5) == Integer.zero() then |
| 855 |
return Integer(5) |
| 856 |
end |
| 857 |
|
| 858 |
local g = function(x) |
| 859 |
local temp = Integer.powmod(x, Integer(2), self) |
| 860 |
return temp |
| 861 |
end |
| 862 |
|
| 863 |
local xstart = Integer(2) |
| 864 |
while xstart < self do |
| 865 |
local x = xstart |
| 866 |
local y = xstart |
| 867 |
local d = Integer.one() |
| 868 |
while d == Integer.one() do |
| 869 |
x = g(x) |
| 870 |
y = g(g(y)) |
| 871 |
d = Integer.gcd((x - y):abs(), self) |
| 872 |
end |
| 873 |
|
| 874 |
if d < self then |
| 875 |
return d |
| 876 |
end |
| 877 |
|
| 878 |
xstart = xstart + Integer.one() |
| 879 |
end |
| 880 |
end |
| 881 |
|
| 882 |
--- Uses Miller-Rabin to determine whether a number is prime up to a very large number. |
| 883 |
local smallprimes = {Integer:new(2), Integer:new(3), Integer:new(5), Integer:new(7), Integer:new(11), Integer:new(13), Integer:new(17), |
| 884 |
Integer:new(19), Integer:new(23), Integer:new(29), Integer:new(31), Integer:new(37), Integer:new(41), Integer:new(43), Integer:new(47)} |
| 885 |
|
| 886 |
function Integer:isprime() |
| 887 |
if self % Integer(2) == Integer.zero() then |
| 888 |
if self == Integer(2) then |
| 889 |
return true |
| 890 |
end |
| 891 |
return false |
| 892 |
end |
| 893 |
|
| 894 |
if self == Integer.one() then |
| 895 |
return false |
| 896 |
end |
| 897 |
|
| 898 |
for _, value in pairs(smallprimes) do |
| 899 |
if value == self then |
| 900 |
return true |
| 901 |
end |
| 902 |
end |
| 903 |
|
| 904 |
local r = Integer.zero() |
| 905 |
local d = self - Integer.one() |
| 906 |
while d % Integer(2) == Integer.zero() do |
| 907 |
r = r + Integer.one() |
| 908 |
d = d / Integer(2) |
| 909 |
end |
| 910 |
|
| 911 |
for _, a in ipairs(smallprimes) do |
| 912 |
local s = r |
| 913 |
local x = Integer.powmod(a, d, self) |
| 914 |
if x == Integer.one() or x == self - Integer.one() then |
| 915 |
goto continue |
| 916 |
end |
| 917 |
|
| 918 |
while s > Integer.zero() do |
| 919 |
x = Integer.powmod(x, Integer(2), self) |
| 920 |
if x == self - Integer.one() then |
| 921 |
goto continue |
| 922 |
end |
| 923 |
s = s - Integer.one() |
| 924 |
end |
| 925 |
do |
| 926 |
return false |
| 927 |
end |
| 928 |
::continue:: |
| 929 |
end |
| 930 |
|
| 931 |
return true |
| 932 |
end |
| 933 |
|
| 934 |
--- Returns the absolute value of an integer. |
| 935 |
--- @return Integer |
| 936 |
function Integer:abs() |
| 937 |
if self.sign >= 0 then |
| 938 |
return Integer(self) |
| 939 |
end |
| 940 |
return -self |
| 941 |
end |
| 942 |
|
| 943 |
----------------- |
| 944 |
-- Inheritance -- |
| 945 |
----------------- |
| 946 |
|
| 947 |
__Integer.__index = EuclideanDomain |
| 948 |
__Integer.__call = Integer.new |
| 949 |
Integer = setmetatable(Integer, __Integer) |
| 950 |
|
| 951 |
---------------------- |
| 952 |
-- Static constants -- |
| 953 |
---------------------- |
| 954 |
|
| 955 |
Integer.FACTORIZATIONLIMIT = Integer(Integer.DIGITSIZE) |