Kontent qismiga oʻtish

Modul:Divisors

Vikipediya, ochiq ensiklopediya

Bu modul uchun Modul:Divisors/doc nomli hujjat sahifasini yaratishingiz mumkin

-- Find an integer's divisors, sum of divisors, and total divisors
-- Gts-tg@el wiki, Aug. 2017

local p = {}

-- return an array of divisors for number n
local function calc(n)
	local limit    = math.sqrt(n)
	local result   = {}
	
	for i=1, limit do
	   if n/i == math.floor(n/i) then
   	   	  result[n/i] = i+1
   	   end
   	   
   	   if n % i == 0 then
   	     result[i] = i
   	   end
    end
    
    local sorted = {}
    for value, key in pairs(result) do table.insert(sorted, value) end
	table.sort(sorted)
    
    return sorted
end

-- return the sum of divisors
function p.sum(frame)
	local n        = frame.args[1]
	local divisors = calc(n)
	local result   = 0
	
	for key, value in pairs(divisors) do
		if value ~= tonumber(n) then
		   result = result + value
		end
	end
	
	if result <= tonumber(os.date('%Y')) then
	   result = '[[' .. result .. ' (son)|'	.. result .. ']]'
	end
	
	return result
end

-- return the total number of divisors
function p.total(frame)
	local n        = frame.args[1]	
	local divisors = calc(n)
	local result   = 0
	
	for key, value in pairs(divisors) do
		if value ~= tonumber(n) then
		   result = result + 1
	    end
	end
	
	return result	
end

-- return a text list of divisors (optionally as wikilinks)
function p.list(frame)
	local n        = frame.args[1]
	local wikilink = frame.args[2]
	
	local divisors = calc(n)

  	local str = ''
	for key, value in pairs(divisors) do
	   if wikilink ~= false and tonumber(value) <= tonumber(os.date('%Y')) and value ~= tonumber(n) then 
	     value = '[[' .. value .. ' (son)|' .. value .. ']]' 	
	   end
	   
	   if value == tonumber(n) then
	   	  value = "" .. value .. ""
	   end
	   
	   str = str .. ' ' .. value
	end
	
	return str
	
end

return p
-- =p.list{args={'3456', true}}