Formally verified solutions to the first 100 Project Euler problems
ID: updates/formally-verified-solutions-to-the-first-100-project-euler-problems
Updates Formally verified solutions to the first 100 Project Euler problems by
Ciro Santilli 40 Created 2026-12-01 Updated 2026-12-02
github.com/cirosantilli/project-euler-solutions#formalsw I've used formal software verification to prove the correctness of the first 100 Project Euler solutions both in Python and Lean.
First I asked Codex to create naive implementations of each problem. These are obvious, often involve brute force search, and would be unfeasibly slow for larger inputs. Making these implementations means choosing the input parameters for each function, and sometimes there are a few different choices possible. I reviewed a certain number of them manually.
These naive implementations then serve as the formal definition of the problem, and the main formal proof that I wanted and achieved was that:
In Lean this is "straightforward": the language is designed so that you can just make such proofs directly inside the language itself which is awesome.
To do that I vibecoded my own Python to Lean system called FormalSW: github.com/cirosantilli/formalsw The system uses the Python interpreter to parse Python and output a JSON data structure which Lean can read. It also has a formal model of many primitive of the Python language. It can then use both of those pieces of information to make formal proofs about programs. The system is a massive hack, incomplete, and probably unsound. But it is there nonetheless.
FormalSW specifications are stored in docstrings, and I'm happy with how terse and human readable they turned out to be. For example, for problem 1 the native version states:
def naive(n: int) -> int:
"""
@formalsw: Requires(n >= 0)
@formalsw: Returns
@formalsw: BigO(n)
"""
if n == 0:
return 0
m = n - 1
s = naive(m)
return s + m if m % 3 == 0 or m % 5 == 0 else swhile the optimized one states:
def solve(n: int) -> int:
"""
@formalsw: Py from project_euler_statements.p1 import naive
@formalsw: Requires(n > 1)
@formalsw: Equiv(naive)
@formalsw: BigO(1)
"""
n -= 1
return sum_of_multiples(3, n) + sum_of_multiples(5, n) - sum_of_multiples(15, n)Some key properties include:
Returns: the function does not throw exceptions or go in infinite loops or has dead or live locks (the system supports multithreading)BigO: proves an upper bound on the worse case time complexity of a functionEquiv: states that two functions are equivalent, i.e. that they have the same output for every input
One alternative which I considered briefly for Python proofs was Nagini github.com/marcoeilers/nagini which achieves similar goals. Nagini represents its proofs in Python itself. But I thought it would be a bit cuter to have a Lean system, so that proofs can be reused across multiple languages.
The next thing I want to do is to vibe-add C to FormalSW and then do corresponding proofs for the first 100 problems. One difficulty which I want to handle is tha C integers have limited width unlike Python which has arbitrary precision arithmetic by default, which makes proofs simpler. Presumably C proofs will have to state over which range of input values the equivalence is valid.
This was of course an useless end goal done just for fun and because "it had to be done", but it did give me a feeling for the current pain that formal software verification is.
New to topics? Read the docs here!