Back to Home11/16/2025, 6:38:37 PM

Z3 API in Python: From Sudoku to N-Queens in Under 20 Lines (2015)

155 points
14 comments

Mood

supportive

Sentiment

positive

Category

tech

Key topics

Z3 API

SMT Solvers

Constraint Programming

Python

Debate intensity20/100

The post showcases the Z3 API in Python for solving constraint satisfaction problems like Sudoku and N-Queens, sparking discussion on its applications and related projects.

Snapshot generated from the HN discussion

Discussion Activity

Moderate engagement

First comment

12h

Peak period

9

Day 1

Avg / period

5

Comment distribution10 data points

Based on 10 loaded comments

Key moments

  1. 01Story posted

    11/16/2025, 6:38:37 PM

    3d ago

    Step 01
  2. 02First comment

    11/17/2025, 6:49:01 AM

    12h after posting

    Step 02
  3. 03Peak activity

    9 comments in Day 1

    Hottest window of the conversation

    Step 03
  4. 04Latest activity

    11/17/2025, 9:21:59 PM

    1d ago

    Step 04

Generating AI Summary...

Analyzing up to 500 comments to identify key contributors and discussion patterns

Discussion (14 comments)
Showing 10 comments of 14
brap
2d ago
1 reply
That’s a very clean API.
cess11
2d ago
It's very close to the SMTLIB API.

Page 19 in https://smt.st/SAT_SMT_by_example.pdf shows an example in both Python and SMTLIB. After looking at a guide like TFA this book is a good next step.

tombert
2d ago
1 reply
Solvers are something that still kind of feel like magic to me. I have done a fair amount of Isabelle, and the "sledgehammer" tool in there (which uses solvers to see if an existing proof can be made to work to solve your subgoal) is something that impresses me every single time I use it.
nextaccountic
1d ago
On same level of abstraction I think it's useful to view those tools as some form of constraint solving. Think how you can solve sudoku with pen and paper by writing out each possibility, then fill squares either because it has only one possibility or by making guesses. After you fill a square, you can then update your possible states (propagate constraints), and repeat until you solve it or hit an impossible solve (in which case you need to backtrack on some earlier guess).

The algorithms differ mainly in how they keep track of all possibilities and how they update them

I think this answer is pretty good https://cstheory.stackexchange.com/a/29428 (take note at the end "sat is csp on boolean domains")

tyilo
2d ago
1 reply
I have created a Python library called "z4-solver" that adds some nice utility functions on top of z3: https://github.com/Tyilo/z4

I always use that instead of the z3-solver directly.

Recursing
2d ago
1 reply
Have you tried to compare Z3 with cvc5? https://cvc5.github.io/docs/cvc5-1.1.2/api/python/pythonic/p...

It offers basically the same API and could be faster in many cases

gignico
2d ago
I was about to comment the same. Z3 always takes all the credit but cvc5 is just as great!
gbacon
2d ago
We care about Z3 because it is a Satisfiability Modulo Theories (SMT) solver.

Satisfiability: In 1971, Stephen A. Cook established that Boolean satisfiability, given an arbitrary Boolean formula whether an assignment to its variables exists that evaluates true, is NP-complete.

https://dl.acm.org/doi/10.1145/800157.805047

Translating between NP-complete problems is at most a polynomial (“fast”) amount of work, so every improvement gained on satisfiability (whose worst case is exponential rather than polynomial time complexity) benefits all other NP-complete problems, and thus the rest of NP.

Modulo Theories: We can think of SMT as a high-level language that automates encoding of other problems into raw Boolean formulas. Applications built on Z3 outsource search by encoding problems via one or more theories and then decoding results back to the problem domain at hand.

The benefits of doing this are (1) using an existing robust, well-tested suite of algorithms where (2) lots of research effort is concentrated and (3) improvements to Z3 improve your problem’s results more-or-less for free. According to Microsoft, “Z3 is used in a wide range of software engineering applications, ranging from program verification, compiler validation, testing, fuzzing using dynamic symbolic execution, model-based software development, network verification, and optimization.”

https://www.microsoft.com/en-us/research/project/z3-3/

See also:

https://github.com/Z3Prover/z3

raphman
2d ago
"SAT/SMT by Example" also contains many Z3 examples (and has a new URL): https://smt.st/
philzook
2d ago
I'm a fan. I've been building a proof assistant directly on the z3py api. https://pypi.org/project/knuckledragger/0.1.3/

4 more comments available on Hacker News

ID: 45947301Type: storyLast synced: 11/19/2025, 6:17:55 PM

Want the full context?

Jump to the original sources

Read the primary article or dive into the live Hacker News thread when you're ready.