Math [Lua]:通过两个表划分数字

Math [Lua]:通过两个表划分数字,math,lua,Math,Lua,我有两张桌子;一个包含未指定数量的数字,以及第二个表。我要做的是让第一个表中的每个数值除以第二个表中的每个数值。我试过以下方法,因为这是我所能想到的唯一可行的方法,但事实并非如此,因为它告诉我,我正在尝试对一个零值执行一个算术运算 function findFactors( num ) local factors = { } local x = 0 while ( x < num ) do x = x + 1 if ( num % x

我有两张桌子;一个包含未指定数量的数字,以及第二个表。我要做的是让第一个表中的每个数值除以第二个表中的每个数值。我试过以下方法,因为这是我所能想到的唯一可行的方法,但事实并非如此,因为它告诉我,我正在尝试对一个零值执行一个算术运算

function findFactors( num )
    local factors = { }
    local x = 0
    while ( x < num ) do
        x = x + 1
        if ( num % x == 0 ) then
            table.insert( factors, "±" .. x )
        end
    end
    return factors
end

function findZeros( a, b, c )
    local zeros = { }
    local constant = findFactors( c )
    if ( a >= 2 ) then
        local coefficient = findFactors(a)
        for _, firstChild in pairs( constant ) do
            for _, secondChild in pairs( coefficient ) do
                local num1, num2 = tonumber( firstChild ), tonumber( secondChild )
                if num1 and num2 then
                    table.insert( zeros, (num1 / num2) )
                end
            end
        end
        print( table.concat (zeros, ",") )
    elseif a < 2 then
        print( table.concat (constant, ",") )
    end
end

findZeros( 3, 4, 6 )
函数findFactors(num)
局部因子={}
局部x=0
而(x=2),则
局部系数=findFactors(a)
对于u,成对的第一个孩子(常数)do
对于u,成对的第二个子项(系数)do
本地num1,num2=tonumber(第一个孩子),tonumber(第二个孩子)
如果num1和num2,那么
表.插入(零,(num1/num2))
结束
结束
结束
打印(table.concat(零,“”,“”)
如果a<2,则
打印(table.concat(常数,“”,“”)
结束
结束
芬泽罗斯(3,4,6)
我似乎找不到一种方法来做我想做的事情,因为我对lua还很陌生。如果您对如何在两个表之间划分数值有任何帮助,我们将不胜感激

table.insert( factors, "±" .. x )
在这里,您将在
因子中插入一个字符串,如
“±1”
“±2”
,等等。这不是有效的数字表示。如果要同时插入正数和负数,请尝试以下操作:

table.insert(factors, x)
table.insert(factors, -x)

注意这里的
x
-x
是数字,而不是字符串,因此您可以在
findZeros
中省略对
tonumber
的调用:表a:1、2、3、6;表B:1、3。如果我使用原始帖子中的代码,它将不会起任何作用。一直以来,我都忘记了我添加的代码,哈哈。谢谢现在可以了。