In 2015, Singapore’s PM Had Written A Sudoku Solver In C++, Current Foreign Minister Had Translated It Into JavaScript

The news of Singapore’s foreign minister building an AI assistant for himself using NanoClaw to answer diplomacy questions has been doing the rounds, but Singapore’s politics has been staffed with some hands-on coders for a while now.

Lee Hsien Loong’s Sudoku Solver

In May 2015, then-Prime Minister Lee Hsien Loong shared on Facebook that the last program he had personally written was a Sudoku solver, coded in C++. He hadn’t written it for a class or a job — it was a personal project, built for the satisfaction of solving a puzzle algorithmically.

“The program is pretty basic: it runs at the command prompt, in a DOS window. Type in the data line by line (e.g. 1-3-8—6), then the solver will print out the solution (or all the solutions if there are several), the number of steps the program took searching for the solution, plus some search statistics,” he had written on Facebook.

The program ran at the command prompt in a DOS window. Users typed in a Sudoku grid line by line, and the solver would print out the solution, the number of steps taken during the search, and some search statistics. Under the hood, it used a backtrack search that chose the next cell to guess by minimising the fanout — a sensible heuristic that reduces unnecessary branching.

Lee shared the source code publicly and even posed a small challenge to readers: if x is a binary integer, what does (x & -x) compute? The answer, as several readers correctly noted, is that it returns the least significant ‘1’ bit of x — the highest power of two that divides x. It is an old bit-manipulation trick, not something Lee invented, but the fact that a sitting prime minister was casually posting bitmask puzzles to his Facebook followers says something about the technical culture Singapore’s leadership had cultivated.

The post was tagged #SmartNation — a reference to Singapore’s national initiative to use technology and data to improve the lives of citizens, which Lee’s government had launched that same year.


Balakrishnan Picks It Up and Translates It

What happened next was even more interesting. Vivian Balakrishnan — now the Foreign Minister, and then Minister-in-charge of the Smart Nation Initiative and Minister for the Environment and Water Resources — saw the post and decided to translate the C++ solver into JavaScript. Not as a government exercise. Just, as he put it, to learn a new language.

“ranslated PM Lee Hsien Loong’s Sudoku solver from C++ to Javascript. Trying to learn a new language. Never realised I would actually miss the static typing of C. Dynamic typing makes writing initial code seductively easy, but difficult to debug,” he wrote on Facebook.

Balakrishnan published the JavaScript version at his personal website and reflected candidly on the experience. He noted that dynamic typing made writing the initial code seductively easy, but difficult to debug — and that he found himself unexpectedly missing the static typing of C. It’s the kind of observation that only comes from someone who actually sat down and wrote the thing, not someone who skimmed a tutorial.

The JavaScript solver generated only the first solution rather than all possible solutions — a deliberate simplification for the port — and still used the same backtrack search logic as Lee’s original.


What This Says About Singapore’s Political Culture

Both Lee and Balakrishnan are medical doctors by training, not computer scientists. Lee studied mathematics at Cambridge before medicine; Balakrishnan is a trained ophthalmologist. Neither had a professional reason to be writing or porting Sudoku solvers in their spare time. They did it anyway.

This is the same Balakrishnan who, a decade later, would build a self-hosted AI assistant running on a Raspberry Pi — complete with a custom knowledge graph, local vector embeddings, Docker container isolation, and on-device voice transcription. The throughline is consistent: learn the technology by building with it, not by commissioning a report about it.

At a time when AI is rapidly automating coding itself and most governments are still figuring out what policies to adopt, Singapore’s leadership has a decade-long habit of actually getting their hands dirty. That is a harder thing to fake than a policy paper, and a more reliable foundation for making good decisions about technology than almost anything else.

The 2015 Facebook exchange between a prime minister sharing bitmask tricks and his minister porting the code to a new language is, in retrospect, a small but sharp preview of the approach that continues today.