Intpow

-- When exponentiating Lua numbers, always prefer the `^` operator (or `math.pow`) over this
-- Divide-and-conquer approach applied to integer exponentiation, intended for custom types like "big" numbers etc.
-- Exponentiation by squaring
local function pow(base, exponent)
	if exponent == 1 then
		return base
	end
	if exponent % 2 == 1 then
		local root = pow(base, (exponent - 1) / 2)
		return root * root * base
	end
	local root = pow(base, exponent / 2)
	return root * root
end

return function(base, exponent)
	if exponent == 0 then
		return 1
	end
	assert(exponent % 1 == 0)
	if exponent < 0 then
		return 1 / pow(base, -exponent)
	end
	return pow(base, exponent)
end
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.