mirror of
https://github.com/SashLilac/cambridge.git
synced 2024-11-22 11:39:03 -06:00
Optimized bigint exponentiation (again)
This commit is contained in:
parent
4920e5de1c
commit
32c2274bef
@ -147,11 +147,11 @@ function bigint.unserialize(big, output_type, precision)
|
||||
|
||||
-- num is the first (precision + 1) digits, the first being separated by
|
||||
-- a decimal point from the others
|
||||
num = num .. big.digits[1]
|
||||
num = num .. math.floor(big.digits[1])
|
||||
if (precision > 1) then
|
||||
num = num .. "."
|
||||
for i = 1, (precision - 1) do
|
||||
num = num .. big.digits[i + 1]
|
||||
num = num .. math.floor(big.digits[i + 1])
|
||||
end
|
||||
end
|
||||
|
||||
@ -451,15 +451,19 @@ function bigint.exponentiate(big, power)
|
||||
local result = bigint.new(1)
|
||||
local base = big:clone()
|
||||
|
||||
while (bigint.compare(exp, bigint.new(0), ">")) do
|
||||
while (true) do
|
||||
if (bigint.compare(
|
||||
bigint.modulus(exp, bigint.new(2)), bigint.new(1), "=="
|
||||
)) then
|
||||
result = bigint.multiply(result, base)
|
||||
end
|
||||
if (bigint.compare(exp, bigint.new(1), "==")) then
|
||||
break
|
||||
else
|
||||
exp = bigint.divide(exp, bigint.new(2))
|
||||
base = bigint.multiply(base, base)
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user