faster __pow()
This commit is contained in:
		
							parent
							
								
									cf9d0892bf
								
							
						
					
					
						commit
						9dd2ed4a80
					
				
							
								
								
									
										24
									
								
								bigint.lua
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								bigint.lua
									
									
									
									
									
								
							@ -207,6 +207,17 @@ local function udec(a)
 | 
				
			|||||||
    trim(a)
 | 
					    trim(a)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					local function upow(a, b)
 | 
				
			||||||
 | 
					    -- https://stackoverflow.com/a/102319/2332397
 | 
				
			||||||
 | 
					    if #b == 1 and b[1] == 0 then return BigInt(1) end
 | 
				
			||||||
 | 
					    local temp = upow(a, b/2)
 | 
				
			||||||
 | 
					    if b[1] % 2 == 0 then
 | 
				
			||||||
 | 
					        return temp * temp
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					        return a * temp * temp
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function BigInt.__eq(a, b)
 | 
					function BigInt.__eq(a, b)
 | 
				
			||||||
    if #a ~= #b then return false end
 | 
					    if #a ~= #b then return false end
 | 
				
			||||||
    if a.sign ~= b.sign then return false end
 | 
					    if a.sign ~= b.sign then return false end
 | 
				
			||||||
@ -289,6 +300,8 @@ function BigInt.__mul(a, b)
 | 
				
			|||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function BigInt.__div(a, b)
 | 
					function BigInt.__div(a, b)
 | 
				
			||||||
 | 
					    local a = BigInt(a)
 | 
				
			||||||
 | 
					    local b = BigInt(b)
 | 
				
			||||||
    local q = udiv(a, b)
 | 
					    local q = udiv(a, b)
 | 
				
			||||||
    q.sign = a.sign * b.sign
 | 
					    q.sign = a.sign * b.sign
 | 
				
			||||||
    trim(q)
 | 
					    trim(q)
 | 
				
			||||||
@ -297,15 +310,8 @@ end
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
function BigInt.__pow(a, b)
 | 
					function BigInt.__pow(a, b)
 | 
				
			||||||
    local a = BigInt(a)
 | 
					    local a = BigInt(a)
 | 
				
			||||||
    local b = BigInt(b):clone()
 | 
					    local b = BigInt(b)
 | 
				
			||||||
    local c = BigInt(1)
 | 
					    return upow(a, b)
 | 
				
			||||||
    local i = BigInt(1)
 | 
					 | 
				
			||||||
    if b.sign == -1 then b.sign = 1 end
 | 
					 | 
				
			||||||
    while i <= b do
 | 
					 | 
				
			||||||
        c = c * a
 | 
					 | 
				
			||||||
        i:inc()
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
    return c
 | 
					 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function BigInt:__tostring()
 | 
					function BigInt:__tostring()
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user