Skip to content

Instantly share code, notes, and snippets.

@HarryR
Created July 7, 2017 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarryR/8e69f948f7c5c94ea077ba67816e720d to your computer and use it in GitHub Desktop.
Save HarryR/8e69f948f7c5c94ea077ba67816e720d to your computer and use it in GitHub Desktop.
Use Z3 solver to find `x` in `√x + √(x+15) = 15`
# -*- coding: utf-8 -*-
from z3 import *
SOLVER = Solver()
# √x
Y = Real("Y")
X = Y * Y
SOLVER.add(Y > 0.0)
# √(x+15)
K = Real("K")
SOLVER.add(K > 0.0)
SOLVER.add(K * K == (X + RealVal("15.0")))
# √x + √(x+15) = 15
SOLVER.add( Y + K == RealVal("15") )
m = SOLVER.check()
if m == sat:
print(SOLVER.model())
else:
print("Cannot solve")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment