Skip to content

Instantly share code, notes, and snippets.

@AlbertVeli
Created April 6, 2020 08:47
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 AlbertVeli/b14713c9eac4f82f1ee3d9bba5d6805c to your computer and use it in GitHub Desktop.
Save AlbertVeli/b14713c9eac4f82f1ee3d9bba5d6805c to your computer and use it in GitHub Desktop.
Apple Banana Pineapple puzzle
#!/usr/bin/env python3
from z3 import *
a, b, p = Reals('a b p')
s = Solver()
s.add((a / (b + p)) + (b / (a + p)) + (p / (a + b)) == 4)
s.add(b != -p)
s.add(a != -p)
s.add(a != -b)
res = s.check()
if res == sat:
# print one solution (many might exist)
m = s.model()
print('apple =', m[a])
print('banana =', m[b])
print('pineapple =', m[p])
else:
# unsat means no solution found
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment