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:
For every input, the optimized algorithm produces the same output as the native one.
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.
For Python, this requires modeling the Python language in a formal system to then make the proofs.
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 s
while 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:
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.