Factorize

-- Prime-factorize a number. Runs in sqrt(n) time. "Naive" implementation as opposed to advanced "sieves".
return function(
	n -- integer to factorize
)
	local factors = {}
	for factor = 2, n ^ 0.5 do
		local count = 0
		while n % factor == 0 do
			count = count + 1
			n = n / factor
		end
		if count > 0 then
			factors[factor] = count
		end
	end
	if next(factors) == nil then -- no factors: number is a prime (or 0 or 1)
		factors[n] = 1
	end
	-- prime factorization as table `[factor] = count`; `{[1] = 1}` for `n=1`
	return factors
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.