Jump to content

Math + Lua question


[M]ister

Recommended Posts

Posted

I have a value x that is dynamic, for each value of x, I have a group of three numbers, which is integers from 1 to x, each group is different from the others, haven't two equal values in the same group, each value of 1 to x, must exist in 3 groups (no more and no less).

Example:
x = 4
so I have:
1,3,4
1,2,4
1,2,3
2,3,4

As you can see in this example, a group is different from the other, and each value of the group, is repeated up to 3 times.
So I would ask the help of you in the solution of this problem through an algorithm, which manages groups knowing the value of x.

Another examples:

Spoiler

 

x = 5
1,3,4
3,4,5
1,2,4
1,2,5
2,3,5

x = 8
1,3,8
1,2,4
1,2,5
3,4,5
3,5,6
4,6,7
6,7,8
7,8,2

 

 

Posted

I tried this, but without success

local grupos = {}
x = 4

for i=1,x do
	cont = 0
	while (cont < 3) do
		ok = true
		ok2 = true
		grupoID = math.random(1,x) -- Random group for insert the value
		if (grupos[grupoID]) then
			if (not string.find(grupos[grupoID], tostring(i))) then -- If not exist the value in the group

				--[[
				-- When I try to avoid the formation of equal groups the script goes into an infinite loop
				for i2=1, #grupos do
					if (i2 ~= grupoID) then
						k = grupos[grupoID]..", "..i
						if (grupos[i2] == k) then
							ok2 = false
							--outputChatBox(k)
							--outputChatBox(grupos[i2])
							--return
							--break
						end
					end
				end
				--]]
				
				if (ok2) then
					cont = cont+1
					grupos[grupoID] = grupos[grupoID]..", "..i
				end
			end
		else
			cont = cont+1
			grupos[grupoID] = tostring(i)
		end
	end
end

 

Posted

I don't get it what do you want to do exactly and how the groups are created. Randomly or somehow.
But if it sure there will be a X somewhere in your groups then you have to just serch for the max value. ( In your examples there was always an x in the groups)

local groups = {
	{1,3,4},
	{3,4,5},
	{1,2,4},
	{1,2,5},
	{2,3,5}
}

function getGroupX (t) 					--Which is the highest value of groups
	local x = false 					--return false if table is empty
	for _,row in pairs(t) do
		for _,number in pairs(row) do
			if(x == false) then
				x = number
			elseif(number > x) then
				x = number
			end
		end
	end

	return x
end

getGroupX(groups) --will return with 5

 

Posted

Do you know name of the algorithm? If you do post the name because its not that easy to make each number repeat only three times.

Do you know name of the algorithm? If you do post it.

Posted (edited)

It's actually not that simple, I already tried to do it twice, but no success. My code so far:

 

local tbl = {}
local x = 5

for row = 1, x do
    tbl[row] = {0, 0, 0} -- create dummy data so we can loop through the rows without error
end

function getNumCount(num) -- get the number of times a digit is in the array
    local count = 0
    for row = 1, x do
        for i = 1, 3 do
           if tbl[row][i] == num then
               count = count + 1 -- increment when we have a match
           end
        end       
    end
    return count
end

function isNumbersAllowed(n1, n2, n3)
    if n1 == n2 or n2 == n3 or n1 == n3 then -- check if all 3 numbers in a row are different
        return false
    end
    return true
end

function isGroupAllowed(n1, n2, n3) -- make sure we dont have 2 of the same rows like 1,2,3 and 2,1,3
    
    local numOccurance = 0
    for row = 1, x do
        for i = 1, 3 do
            if tbl[row][i] == n1 or tbl[row][i] == n2 or tbl[row][i] == n3 then -- if a column in a row is equial to any of the 3 random numbers generated
                numOccurance = numOccurance + 1 -- count the number of matches
            end
        end
        if numOccurance > 2 then -- we have 2 of the same rows, dont let it insert
            return false
        else
            numOccurance = 0 -- make sure its initialized to 0 after checking a row
        end
    end
    return true
end

function seedTable()
  
    for row = 1, x do
        local num1, num2, num3 
        repeat
            num1 = math.random(1, x) -- get 3 random numbers between 1 and x
            num2 = math.random(1, x)
            num3 = math.random(1, x)
        until isNumbersAllowed(num1, num2, num3) and isGroupAllowed(num1, num2, num3) and getNumCount(num1) < 3 and getNumCount(num2) < 3 and getNumCount(num3) < 3
        -- shouldnt be 2 of the same groups or 1 digit occuring twice in the same row
        print(num1 .. " " .. num2 .. " " .. num3)
        tbl[row] = {num1, num2, num3} -- these 3 numbers passed all of our checks, put them in the table
    end
    
end

seedTable()

It's not working 100%, I can only get it to create x - 1 rows because the last row never has 3 digits that occurred less than 3 times, it's quite tricky.

Edited by pa3ck
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...