Skip to content

Commit ec12fd0

Browse files
committed
fast max_for_op
1 parent 183bb17 commit ec12fd0

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

big-num/omeganum.lua

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,8 @@ function Big:tetrate(other)
919919
return r;
920920
end
921921

922+
local maxoparray = { __index = setmetatable({ 10e9 }, { __index = function () return 8 end }) }
923+
922924
--- @return t.Omega
923925
function Big:max_for_op(arrows)
924926
if Big.is(arrows) then
@@ -934,23 +936,9 @@ function Big:max_for_op(arrows)
934936
return B.TETRATED_MAX_SAFE_INTEGER
935937
end
936938

937-
local arr = {}
938-
arr[1] = 10e9
939-
arr[arrows] = R.MAX_SAFE_INTEGER - 2
940-
for i = 2, math.min(arrows - 1, 1e2) do
941-
arr[i] = 8
942-
end
943-
if arrows > 1e2 then
944-
local limit = math.floor(math.log(arrows, 10))
945-
for i = 6, limit do
946-
arr[10^i] = 8
947-
end
948-
end
949-
arr[arrows - 1] = 8
950-
951939
local res = Big:new({0})
952940
res.asize = arrows
953-
bigs[res] = arr
941+
bigs[res] = setmetatable({ [arrows] = R.MAX_SAFE_INTEGER - 2 }, maxoparray)
954942
return res
955943
end
956944

@@ -991,33 +979,33 @@ function Big:arrow(arrows, other)
991979
end
992980

993981
--remove potential error from before
994-
local arrowsNum = math.floor(arrows)
995-
if (other == 2) then
996-
return self:arrow(arrowsNum - 1, self)
997-
end
998-
local limit_plus = Big:max_for_op(arrowsNum+1)
999-
local limit = Big:max_for_op(arrowsNum)
1000-
local limit_minus = Big:max_for_op(arrowsNum-1)
982+
local arrowint = math.floor(arrows)
983+
if (other == 2) then return self:arrow(arrowint - 1, self) end
984+
985+
local limit_plus = Big:max_for_op(arrowint+1)
986+
local limit = Big:max_for_op(arrowint)
987+
local limit_minus = Big:max_for_op(arrowint-1)
1001988
if (self:max(other):gt(limit_plus)) then
1002989
return self:max(other)
1003990
end
991+
1004992
local r = nil
1005993
if (self:gt(limit) or other > B.MAX_SAFE_INTEGER) or arrows >= 350 then --just kinda chosen randomly
1006994
if (self:gt(limit)) then
1007995
r = self:clone()
1008996
local w = r:get_array()
1009-
w[arrowsNum + 1] = w[arrowsNum + 1] - 1
1010-
if arrowsNum < 25000 then --arbitrary, normalisation is just extra steps when you get high enough
997+
w[arrowint + 1] = w[arrowint + 1] - 1
998+
if arrowint < 25000 then --arbitrary, normalisation is just extra steps when you get high enough
1011999
r:normalize()
10121000
end
10131001
elseif (self:gt(limit_minus)) then
1014-
r = Big:create(self:get_array()[arrowsNum])
1002+
r = Big:create(self:get_array()[arrowint])
10151003
else
10161004
r = B.ZERO
10171005
end
10181006
local j = r:add(other)
10191007
local w = j:get_array()
1020-
w[arrowsNum+1] = (w[arrowsNum+1] or 0) + 1
1008+
w[arrowint+1] = (w[arrowint+1] or 0) + 1
10211009
j:normalize()
10221010
return j
10231011
end
@@ -1039,7 +1027,7 @@ function Big:arrow(arrows, other)
10391027
f = 0
10401028
end
10411029
local w = r:get_array()
1042-
w[arrowsNum] = (w[arrowsNum] or 0) + f
1030+
w[arrowint] = (w[arrowint] or 0) + f
10431031
r:normalize()
10441032
return r
10451033
end

0 commit comments

Comments
 (0)