代码拉取完成,页面将自动刷新
#!/usr/bin/env python3
import z3
from itertools import chain
"""
A square array of numbers, usually positive integers, is called a magic square
if the sums of the numbers in each row, each column, and both main diagonals are the same.
Magic square of 15 looks like:
8 1 6
3 5 7
4 9 2
"""
x = [[z3.Int(f'x_{i}_{j}') for i in range(3)] for j in range(3)]
range_c = [z3.And(i >= 1, i <= 9) for i in chain(*x)]
unique_c = [z3.Distinct([i for i in chain(*x)])]
row_c = [sum(i) == 15 for i in x]
column_c = [sum(list(chain(*x))[i::3]) == 15 for i in range(3)]
diagonal_c = [x[0][0]+x[1][1]+x[2][2] == 15,
x[0][2]+x[1][1]+x[2][0] == 15]
magic_c = range_c + unique_c + row_c + column_c + diagonal_c
solver = z3.Solver()
solver.add(magic_c)
if solver.check() == z3.sat:
m = solver.model()
r = [[m.evaluate(x[i][j]) for i in range(3)] for j in range(3)]
for i in r:
print(' '.join(map(str, i)))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。