Skip to content

Instantly share code, notes, and snippets.

@shahrilnet
Last active June 25, 2021 00:16
Show Gist options
  • Save shahrilnet/18d2f0e829bba15089420e709353fe1e to your computer and use it in GitHub Desktop.
Save shahrilnet/18d2f0e829bba15089420e709353fe1e to your computer and use it in GitHub Desktop.
Generating list of valid prime numbers using Z3 theorem prover
'''
Using Z3 to check if the number is prime
Original reference: https://stackoverflow.com/a/35653749/1768052
'''
from z3 import *
def isPrime(x):
y, z = Ints("y z")
return And(x > 1, Not(Exists([y, z], And(y < x, z < x, y > 1, z > 1, x == y*z))))
s = Solver()
x = Int('x')
s.add(isPrime(x))
while s.check() == sat:
ans = s.model()[x]
print(ans)
s.add(x != ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment