Skip to content

Commit 5e31635

Browse files
authored
Create z3_solver.py
1 parent 9af4e06 commit 5e31635

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

z3_solver.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from z3 import Solver, Int ,set_param
2+
from gmpy2 import isqrt
3+
set_param('parallel.enable', True)
4+
5+
def z3_solve(self, n, timeout_amount):
6+
s = Solver()
7+
s.set("timeout", timeout_amount * 1000)
8+
p = Int("x")
9+
q = Int("y")
10+
i = int(isqrt(n))
11+
if i**2 == n: # check if we are dealing with a perfect square otherwise try to SMT.
12+
return i,i
13+
s.add(p * q == n, p > 1, q > i, q > p) # In every composite n=pq,there exists a p>sqrt(n) and q<sqrt(n).
14+
try:
15+
s_check_output = s.check()
16+
res = s.model()
17+
return res[p].as_long(), res[q].as_long()
18+
except:
19+
return None, None
20+
21+
print(z3_solve(int(sys.argv[1])))

0 commit comments

Comments
 (0)