Last active
June 25, 2021 00:16
-
-
Save shahrilnet/18d2f0e829bba15089420e709353fe1e to your computer and use it in GitHub Desktop.
Generating list of valid prime numbers using Z3 theorem prover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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