示例#1
0
from z3 import Solver, Ints, And, sat
s = Solver()

# Declare three integer variables/constants of Z3Py {x, y, z} :
x, y, z = Ints('x y z')

# Assert that {x, y, z} are positive integers such that 0 < x < y < z :
s.add(And(0 < x, x < y, y < z))

# Assert that x*x + y*y = z*z, i.e. (x,y,z) is a Pythagorean triple :
# s.add ( x % 2 != 0)
# s.add ( y % 2 == 0)
# s.add ( y == z - 1)
s.add(x * x + y * y == z * z, y == z - 2)

n = 1
results = []
while s.check() == sat and n <= 20:
    m = s.model()
    results.append(m)
    s.add(x != m[x])
    n = n + 1

# print len (results)

for p in range(len(results)):
    print(results[p])
示例#2
0
            GDDeg = GDDeg - 360
            
        if (GDDeg < (-180)):
            GDDeg = GDDeg + 360
##    print ("GDDeg:",GDDeg)
##    print ("#################################")
    return [Dist, DDeg, GDDeg]
    


true = BoolSort().cast(True)
false = BoolSort().cast(False)

reset = Bool('reset')
BInRobot = Bool('BInRobot')
forward,spin, Su = Ints('forward spin Su')
dist = Real( 'dist')
degToBall = Real( 'degToBall')
degToGoal = Real( 'degToGoal')

# B-Threads
global TooFar, TooClose, MaxPower, MaxSpin, MaxAngToBall, MaxAngToGoal

TooFar=5
TooClose=3.7
MaxPower=100
MaxSpin=100
MaxAngToBall=10
MaxAngToGoal=3

def bounds():
示例#3
0
from z3 import Bools, Ints, Exists, ForAll, Or, And, Implies, Not
from z3 import Tactic, Then, OrElse, Repeat, ParThen
from z3 import solve

n, p, q, x, y = Ints('n p q x y')

middle = Or(
    And(-q + y + 1 == 0, -q + x + 1 == 0, -p + q - 2 >= 0, p >= 0,
        n - q - 1 >= 0),
    And(-q + x + 1 == 0, -p + y - 1 >= 0, q - y - 2 >= 0, p >= 0, n - q >= 0),
    And(-n + y + 1 == 0, -n + x + 1 == 0, -n + q == 0, p >= 0, n - p - 2 >= 0),
    And(-q + y + 1 == 0, -q + x + 1 == 0, p + 1 == 0, q - 1 >= 0,
        n - q - 1 >= 0),
    And(-q + x + 1 == 0, p + 1 == 0, y >= 0, q - y - 3 >= 0, n - q - 4 >= 0),
    And(-n + y + 1 == 0, -n + x + 1 == 0, -n + q == 0, p + 1 == 0, n - 1 >= 0),
    And(-p + x == 0, -q + y + 1 == 0, -p + q - 2 >= 0, p >= 0, n - p - 3 >= 0,
        n - q >= 0),
    And(-p + x == 0, -p + y - 1 >= 0, q - y - 1 >= 0, p >= 0, n - q >= 0,
        n - y - 2 >= 0),
    And(-n + y + 1 == 0, -n + q == 0, -p + x == 0, p >= 0, n - p - 2 >= 0),
    And(-n + q == 0, -p + x == 0, -p + y - 1 >= 0, p >= 0, n - y - 2 >= 0),
    And(-q + y + 1 == 0, -p + q - 2 >= 0, p - x - 1 >= 0, p + 1 >= 0,
        n - q >= 0, n + q - x - 6 >= 0, n + q - 4 >= 0),
    And(-p + y - 1 >= 0, q - y - 1 >= 0, p - x - 1 >= 0, p + 1 >= 0,
        n - q >= 0, n - y - 2 >= 0),
    And(-n + y + 1 == 0, -n + q == 0, x >= 0, p - x - 1 >= 0, n - p - 2 >= 0),
    And(-n + q == 0, -p + y - 1 >= 0, x >= 0, p - x - 1 >= 0, n - y - 2 >= 0),
    And(-q + y + 1 == 0, -p + x - 1 >= 0, q - x - 2 >= 0, p >= 0, n - q >= 0),
    And(-p + y - 1 >= 0, -p + x - 1 >= 0, q - x - 1 >= 0, q - y - 1 >= 0,
        p >= 0, n - q >= 0, n - x - 2 >= 0, n - y - 2 >= 0,
        3 * n - q - x - y - 5 >= 0, 4 * n - p - 2 * q - 8 >= 0),
示例#4
0
文件: lab.py 项目: jm5833/offsec
from z3 import Ints, Solver


def sumof(a, b, c, d, e, f, g, h, i, j):
    return ((a * 227) + (b * 249) + (c * 104) + (d * 19) + (e * 33) +
            (f * 169) + (g * 154) + (h * 58) + (i * 98) + (j * 198) == 9595)


a, b, c, d, e, f, g, h, i, j = Ints('a b c d e f g h i j')
solve = sumof(a, b, c, d, e, f, g, h, i, j)
s = Solver()
s.add(solve == True)
s.add(a > 1, b >= 0, c >= 0, d >= 0, e >= 0, f >= 0, g >= 0, h >= 0, i >= 0,
      j >= 0)
s.add(a <= 7, b <= 7, c <= 7, d <= 7, e <= 7, f <= 7, g <= 8, h <= 8, i <= 8,
      j <= 8)
print s.check()
print s.model()
'''
nnnRnnnnLRLnRLnnLLLn
'''
示例#5
0
######### $ python3 pythagorean_triples.py

######### Run from the Python prompt '>>>' at the command line:
######### >>> execfile ('pythagorean_triples.py')           # in Python2 only
######### >>> exec(open("./pythagorean_triples.py").read()) # in Python2 and Python3

from z3 import Solver, Ints, And, Not, Exists, ForAll, sat
import math

s = Solver()

# def isPrime(x):
#     w, v = Ints("w v")

# Declare three integer variables/constants of Z3Py {x, y, z} :
x, y, z, w, v = Ints('x y z w v')

# Assert that {x, y, z} are positive integers such that 0 < x < y < z :
s.add(And(0 < x, x < y, y < z))

# Assert that x*x + y*y = z*z, i.e. (x,y,z) is a Pythagorean triple :
s.add(x * x + y * y == z * z)

#triple is tight
s.add(y + 1 == z)

s.add(
    And(x > 1, Not(Exists([w, v], And(w < x, v < x, w > 1, v > 1,
                                      x == w * v)))))
s.add(x < 100)
# in the same order -- the result of some obvious non-determinism
# and randomness in Z3 implementation). For n >= 25 or 30, this
# will run for several seconds before returning an output.

# Run from the Unix prompt '$' at the command line by issuing:
# $ python3 pythagorean_triples.py

# Run from the Python prompt '>>>' at the command line:
# >>> execfile ('pythagorean_triples.py')           # in Python2 only
# >>> exec(open("./pythagorean_triples.py").read()) # in Python2 and Python3

from z3 import Solver, Ints, And, sat, Exists, Not
s = Solver()

# Declare three integer variables/constants of Z3Py {x, y, z} :
x, y, z, b = Ints('x y z b')

# Assert that {x, y, z} are positive integers such that 0 < x < y < z :
s.add(And(1 < x, b < x, b > 1, x < y, y < z))

# Assert that x*x + y*y = z*z, i.e. (x,y,z) is a Pythagorean triple :
s.add(x * x + y * y == z * z)

# Successive numbers are coprime
# primitive triple is x = a^2 - b^2, y = 2ab, z = a^2 + b^2 where successive(a,b)
# so lets say a = b + 1,
# x = 2 * b + 1, y = b * (b + 1), z = 2 * b * b + 2 * b + 1
s.add(And(x == 2 * b + 1, y == 2 * b * (b + 1), z == 2 * b * b + 2 * b + 1))
n = 1
results = []
while s.check() == sat and n <= 10:
示例#7
0
def solve(problem: str) -> int:
    board = parse_board(iter(problem))
    n = len(board)

    solver = Solver()

    def num(c):
        return ord(c) - ord('A')

    # Set up parameters
    L = Ints('A B C D E F G H I J')
    X: List[List[Int]] = [[None] * n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            b = board[i][j]
            if isa(b, Wall):
                continue
            if b == "":
                X[i][j] = Int(f"x{i}{j}")
            else:
                X[i][j] = L[num(b)]

    # Add constraints
    solver.add(Distinct(L))
    solver.add([And(0 <= l, l <= 9) for l in L])
    for i in range(n):
        for j in range(n):
            if X[i][j] is not None:
                solver.add(And(1 <= X[i][j], X[i][j] <= 9))
    for i in range(n):
        for j in range(n):
            b = board[i][j]
            if not isinstance(b, Wall):
                continue
            for d in range(2):
                s, di, dj = (b.h, 0, 1) if d == 0 else (b.v, 1, 0)
                if not s:
                    continue
                solver.add(L[num(s[0])] != 0)  # no leading zeros
                hint = 0
                for c in s:
                    hint *= 10
                    hint += L[num(c)]
                targets = []
                for k in range(1, n):
                    ni = i + di * k
                    nj = j + dj * k
                    if ni >= n or nj >= n or (X[ni][nj] is None):
                        break
                    targets.append(X[ni][nj])

                solver.add(hint == sum(targets))
                solver.add(Distinct(targets))

    if solver.check() != sat:
        print("failed to solve")
        return 0

    answer_keys = []
    for _ in range(100):
        m = solver.model()

        answer_keys.append(answer_key(m, L))

        add_constraint(solver, m)
        if solver.check() != sat:
            break

    if len(answer_keys) > 1:
        print(f"multiple solutions: {problem} -> {answer_keys}")

    return answer_keys[-1]
"""From math stackexchange:
the formula is as follows:
𝑎=𝑚^2 − 𝑛^2, 𝑏=2𝑚𝑛, 𝑐=^2 + 𝑛^2

where 𝑚>𝑛>0

for any 2 values of 𝑚 and 𝑛 the above formula will give a Pythagorean Triple.

To get a Primitive Pythagorean triple, 𝑚 and 𝑛 have to co-prime and not both odd.
"""
def Odd(x):
    return x % 2 == 1

# Declare three integer variables/constants of Z3Py {x, y, z} :
x, y, z, a, b = Ints ('x y z a b')

# Assert that {x, y, z} are positive integers such that 0 < x < y < z :
s.add (And( 0 < x , x < y , y < z ) )

# Assert that x*x + y*y = z*z, i.e. (x,y,z) is a Pythagorean triple :
s.add ( x * x + y * y == z * z )

#m^2 + n^2 = (n + 1)^2
s.add(x * x + y * y == (y+1) * (y+1))

#a is odd and greater than 1
s.add(And(Odd(x), x >= 1))


n = 1