auto 7ˆl–ß=¿JÕ4Ÿº‚„ ƒp Ü ªbÑ¿�þ\ O¬ÙdŒ‰‘I#@þ „O�¿ÁÀ¿?÷--- @class Integer --- Represents an element of the ring of integers. --- @field self table --- @field sign number Integer = {} __Integer = {} -------------------------- -- Static functionality -- -------------------------- -- 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. -- For multiplication to work properly, however, this also must be even so we can take the square root of the digit size exactly. -- 10^14 is still larger than 2^26, so it is still efficient to do multiplication this way. Integer.DIGITLENGTH = 14 -- 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. Integer.DIGITSIZE = 10 ^ Integer.DIGITLENGTH -- Partition size for multiplying integers so we can get both the upper and lower bits of each digits Integer.PARTITIONSIZE = math.floor(math.sqrt(Integer.DIGITSIZE)) --- Method for computing the gcd of two integers using Euclid's algorithm. --- @param a Integer --- @param b Integer --- @return Integer function Integer.gcd(a, b) while b ~= Integer.zero() do a, b = b, a%b end return a end --- Method for computing the gcd of two integers using Euclid's algorithm. --- Also returns Bezout's coefficients via extended gcd. --- @param a Integer --- @param b Integer --- @return Integer, Integer, Integer function Integer.extendedgcd(a, b) local oldr, r = a, b local olds, s = Integer.one(), Integer.zero() local oldt, t = Integer.zero(), Integer.one() while r ~= Integer.zero() do local q = oldr // r oldr, r = r, oldr - q*r olds, s = s, olds - q*s oldt, t = t, oldt - q*t end return oldr, olds, oldt end --- Method for computing the larger of two integers. --- Also returns the other integer for sorting purposes. --- @param a Integer --- @param b Integer --- @return Integer, Integer function Integer.max(a, b) if a > b then return a, b end return b, a end --- Method for computing the smaller of two integers. --- Also returns the other integer for sorting purposes. --- @param a Integer --- @param b Integer --- @return Integer, Integer function Integer.min(a, b) if a < b then return a, b end return b, a end --- Methods for computing the larger magnitude of two integers. --- Also returns the other integer for sorting purposes, and the number -1 if the two values were swapped, 1 if not. --- @param a Integer --- @param b Integer --- @return Integer, Integer, number function Integer.absmax(a, b) if b:ltabs(a) then return a, b, 1 end return b, a, -1 end -- Returns the ceiling of the log base (defaults to 10) of a. -- In other words, returns the least n such that base^n > a. --- @param a Integer --- @param base Integer --- @return Integer function Integer.ceillog(a, base) base = base or Integer(10) local k = Integer.zero() while (base ^ k) < a do k = k + Integer.one() end return k end --- Returns a ^ b (mod n). This should be used when a ^ b is potentially large. --- @param a Integer --- @param b Integer --- @param n Integer --- @return Integer function Integer.powmod(a, b, n) if n == Integer.one() then return Integer.zero() else local r = Integer.one() a = a % n while b > Integer.zero() do if b % Integer(2) == Integer.one() then r = (r * a) % n end a = (a ^ Integer(2)) % n b = b // Integer(2) end return r end end --- @return RingIdentifier local t = {ring=Integer} t = setmetatable(t, {__index = Integer, __eq = function(a, b) return a["ring"] == b["ring"] end, __tostring = function(a) return "ZZ" end}) function Integer.makering() return t end ---------------------------- -- Instance functionality -- ---------------------------- -- So we don't have to copy the Euclidean op