P:S:我从this元线程了解到,在堆栈溢出时也可以询问代码解释。
我试图从this网站了解亚里士多德数谜的解算器。我理解这一点,直到我们使用高斯消去法进行行缩减时,我发现了以下几点:

a = 76 - j - k - n - 2o - p - r - s
b = j + n + o
c = -38 + k + o + p + r + s
d = j + k + o
e = -38 + k + n + o + p + r
f = 38 - j - k - n - o - p
g = 38 - k - o - r
h = -38 + n + o + p + r + s
i = 38 - j - k - n - o - r
l = 38 - p - s
m = 38 - n - o - p
q = 38 - r - s

代码的作者接着说:
现在,从{1,2,…,19}中取出每个大小为7的排列,将其赋给自变量,生成依赖变量,并根据约束条件进行测试,直到找到解。
我真的不明白这个概念。尤其是在thisc文件中,我不理解以下两个函数:
bool next_permutation(void)
{
    for (int x = 6; x >= 0; x--) {
        indices[x]++;

        if (indices[x] == 19) {
            if (!x) {
                return false;
            }
            moveback(x, 18);
            indices[x] = x;
            continue;
        }

        swap(x, indices[x]);
        break;
    }

    j = elem[0];
    k = elem[1];
    n = elem[2];
    o = elem[3];
    p = elem[4];
    r = elem[5];
    s = elem[6];

    return true;
}



// adds values to set
// returns true if value successfully added; false otherwise
bool add(int value)
{
    if (value > 19 || value < 1) {
        return false;
    }

    int bit = 1 << value;
    if (set & bit) {
        return false;
    }

    set |= bit;
    return true;
}

如果有人能帮助我理解这个解算器,我将不胜感激。注意,作者使用python脚本进行行缩减。

最佳答案

我认为Gaussian elimination只是一种通过将一个方程代入另一个方程以消除不必要的变量来减少一组方程的方法。例如,可以将19个方程式中的第一个方程式转换为:

a = 38 - b - c

然后把它换成另外18个。这将消除a,并将方程式减少到18。然后重复,直到你不能再消除。
这是一个很好的问题,它给了我一个尝试SymPy软件包的借口。
下面是一些代码,它在symphy中创建方程,然后使用symphy的solve函数来减少方程。
from sympy import symbols, solve
from sympy.parsing.sympy_parser import parse_expr

# Number of unknowns
n = 19

variable_names = [chr(c + 97) for c in range(19)]

# Create SymPy variables
variables = symbols(variable_names)

print("\n{} unknowns:".format(len(variables)))
print(variables)

# These strings define the equations to be solved
hexagon_rows = [
    'abc',
    'defg',
    'hijkl',
    'mnop',
    'qrs',
    'cgl',
    'bfkp',
    'aejos',
    'dinr',
    'hmq',
    'lps',
    'gkor',
    'cfjnq',
    'beim',
    'adh'
]

def make_expression(chars, rhs=0):
    return parse_expr('+'.join(list(chars)) + '-' + str(rhs))

expressions = []
for chars in hexagon_rows:
    expressions.append(make_expression(chars, 38))

print("\n{} equations to solve:".format(len(expressions)))

for expr in expressions:
    print("{} = 0".format(expr))

# Try to solve the equations
# (They can't be solved but SymPy reduces them down)
reduced_expressions = solve(expressions)

print("\nReduced to {} equations:".format(len(reduced_expressions)))
for var, expr in reduced_expressions.items():
    print("{} = {}".format(var, expr))

输出:
19 unknowns:
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s]

15 equations to solve:
a + b + c - 38 = 0
d + e + f + g - 38 = 0
h + i + j + k + l - 38 = 0
m + n + o + p - 38 = 0
q + r + s - 38 = 0
c + g + l - 38 = 0
b + f + k + p - 38 = 0
a + e + j + o + s - 38 = 0
d + i + n + r - 38 = 0
h + m + q - 38 = 0
l + p + s - 38 = 0
g + k + o + r - 38 = 0
c + f + j + n + q - 38 = 0
b + e + i + m - 38 = 0
a + d + h - 38 = 0

Reduced to 12 equations:
j = b - n - o
k = e - n - o - p - r + 38
l = -p - s + 38
i = -b - e + n + o + p
c = e - n + s
f = -b - e + n + o + r
g = -e + n + p
q = -r - s + 38
m = -n - o - p + 38
h = n + o + p + r + s - 38
d = b + e - 2*n - o - p - r + 38
a = -b - e + n - s + 38

希望能有所帮助。至少是第一部分!

关于python - 亚里斯多德数谜解释,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46249062/

10-13 03:56