Módulo:Collatz
Este módulo permite obtener la Conjetura de Collatz, pasando como parámetro un número arbitrario Plantilla:Param, e irá iterando por cada número de inicio desde el 1 hasta el número dado. Con los datos obtenidos, crea un diagrama de puntos usando la extensión Graph, donde el eje X es el número de pasos y el eje Y es el valor de cada iteración (X0 es el número de inicio). Se ha establecido un valor máximo para Plantilla:Param de 999 como un límite práctico; números mayores causarán problemas de memoria de Lua.
Uso
{{#invoke:Collatz|main|start=12}}
Plantilla:Gráfico
Donde,
- Plantilla:Param es el máximo número de inicio;
- Plantilla:Param es el ancho del gráfico;
- Plantilla:Param es el alto del gráfico.
Funciones
- Plantilla:Code, función que hace una iteración entre 1 y Plantilla:Param, pasando ese valor a la función Plantilla:Code en cada iteración, y formatea un gráfico de lineas con los s datos obtenidos.
- Plantilla:Code, la función para obtener la conjetura de Collatz propiamente dicha, a partir de un número de inicio, Plantilla:Param.
--[[
Módulo para obtener la Conjeutura de Collatz y formatear un gráfico
Licencia: Unlicense (Dominio público)
--]]
local p = {}
local Template = 'Gráfico'
local StartLegend = 'Inicio'
function p.main(frame)
local MaxStart = math.floor(tonumber(frame.args.start) or 27) -- Max start number
local width = math.floor(tonumber(frame.args.width) or 800) -- Graph width
local height = math.floor(tonumber(frame.args.height) or 600) -- Graph height
if MaxStart < 1000 then -- More than 1000 will cause memory issues
local Data = {}
local Highest = {}
local Tags = {}
table.insert(Tags, '|legend=' .. StartLegend)
for num = 1,MaxStart,1 do
local Values = Collatz(num)
table.insert(Highest, #Values)
table.insert(Data, '|y'..num..'=' .. Values)
table.insert(Tags, '|y'..num..'Title=' .. num)
end
table.sort(Highest)
Highest = Highest[#Highest]
local axisX = {}
for v = 0, Highest, 1 do
table.insert(axisX, v)
end
axisX = table.concat(axisX, ',')
axisY = table.concat(Data)
if #Tags > 40 then Tags = {} end
Tags = table.concat(Tags)
if width and height and axisX and axisY and Tags then
return frame:preprocess('{{' .. Template .. '|width='.. width .. '|height='.. height .. '|x='.. axisX .. axisY .. Tags .. '}}')
end
end
return '<span class="error">Se ha proporcionado un número de inicio mayor al permitido</span>'
end
function Collatz(num)
if type(num) == 'number' then
num = math.floor(num)
if num > 0 then
local Values = {num}
while(num > 1) do
if (num % 2 == 0) then
num = (num / 2)
else
num = (num * 3 + 1)
end
table.insert(Values, num)
end
return table.concat(Values, ',')
end
end
end
return p