
SQL query to generate the Mandelbrot set as ASCII art - mmoez
https://sqlite.org/lang_with.html#mandelbrot
======
_aleph2c_
"Udo of Aachen" was the first fictional monk to find the "Mandelbrot" set:

[https://users.math.yale.edu/public_html/People/frame/Fractal...](https://users.math.yale.edu/public_html/People/frame/Fractals/MandelSet/MandelMonk/MandelMonk.html)

~~~
dvt
Fantastic (and hilarious) read :) Thanks for sharing!

------
ThePhysicist
I solved the “eight queens” Problem in SQL once:

[https://gist.github.com/adewes/5e5397b693eb50e67f07](https://gist.github.com/adewes/5e5397b693eb50e67f07)

Recursive common table expressions are quite powerful.

------
doersino
Since SQL (with recursive CTEs and window functions) is Turing complete [1],
one can implement all kinds of algorithms with it. I've replicated an ancient
handwriting recognizer in Postgres:

[https://github.com/doersino/handwriting](https://github.com/doersino/handwriting)

[1]: [https://stackoverflow.com/questions/900055/is-sql-or-even-
ts...](https://stackoverflow.com/questions/900055/is-sql-or-even-tsql-turing-
complete)

------
rajangdavis
The sudoku solver is equally impressive, didn't realize that SQL could be bent
to do these sorts of things.

~~~
GuB-42
The most interesting part for me is that sudoku is a NP-complete problem, and
it can do it rather quickly.

I wonder how effective sqlite is effective at solving SAT or other similar
problems. I mean, there are solvers that are certainly much better, but the
advantage is that you may have sqlite for free in your work environment. The
same can be said of regexes BTW. PCRE are NP-hard and have them solve SAT is
relatively straightforward.

~~~
Twisol
Sudoku is only NP-complete if you generalize to arbitrary NxN boards. The
standard 9x9 Sudoku gives a finite class of problem instances, so you could
pre-generate a finite lookup table and get an O(1) algorithm on that class.

It sounds like I'm nitpicking, but restrictions of any NP-Complete problem to
a finite class automatically reduces the strength of the problem. To the
point, an SQL statement on "small enough" SAT instances will probably also be
rather fast.

~~~
chx
> you could pre-generate a finite lookup table and get an O(1) algorithm on
> that class.

If I remember my numbers right there are ~6.67 * 10 ^ 21 valid puzzles so if
you want to generate and store that table, I am afraid your Amazon S3 bill
will be a bit on the higher side. According to
[https://www.bernardmarr.com/default.asp?contentID=1846](https://www.bernardmarr.com/default.asp?contentID=1846)
, the ‘Global Datasphere’ in 2018 reached 18 zettabytes. IOW, your little
lookup table would consume all the storage in the world.

So yes in theory you are right of course, it's doable but in practice it's
not.

~~~
hinkley
I'm not sure that's the finite lookup table he's talking about. Within the
last year we talked about Peter Norvig's sudoku solver:
[https://norvig.com/sudoku.html](https://norvig.com/sudoku.html)

He starts by generating a table where each cell is represented by a data
structure that enumerates all of the legal values in that cell. The options.
Then there is code that can find all the peers of a cell (same row, same
column, same square).

Turns out that most 'easy' and many 'moderate' puzzles can be solved by
starting with a list of all cells with one option.

For each cell:

* solve the cell

* for all peers, eliminate that value from their options

* for all peers with one remaining option, recurse

I believe that's quadratic time, and intuition tells me breadth-first
converges faster.

After that, Norvig reaches for brute force, under the theory that this has
already gotten you so close to the solution that it's not worth implementing
and debugging more complex constaint logic. But I think that despite his
claims that Sudoku is a disease, I think this is a taunt; it simply encourages
you to go into a more sophisticated process of elimination.

Single elimination: Any option that only has one cell in a row, column or
square: solved.

Double elimination: If a pair of peers have only the same two options, then
eliminate that option from all of their common peers. Solves nothing, but may
solve other cells.

Triple and Quadruple elimination: same thing, but progressively rarer.

Quintuple elimination: I don't recall why, but it was asserted that since this
is more than half of 9, any time this happens a simpler constraint is also
true.

I believe that covers all of the moderate difficulty puzzles and the
occasional hard ones. From there there's a whole array of other constraints
you can implement. You can either do it yourself as an exercise, or there are
whole websites dedicated to these rules.

~~~
Twisol
This is fascinating in its own right, of course, but I was indeed referring to
a literal finite lookup table, by appeal to the finitude of the 9x9 problem
class only. You give a useful strategy for compacting the size of the lookup
table though!

(And really, isn't a function just a compressed lookup table? Often trading
time for size.)

Another theoretically interesting way to shrink the lookup table is to reduce
all grids to the set of _essentially different grids_ \-- see Wikipedia for
the details [0]. The upshot is a reduction in size to "only" 5,472,730,538
grids. You can then scan the set for a grid matching your input clues --
though, as noted, this trades time for size.

[0]
[https://en.wikipedia.org/wiki/Mathematics_of_Sudoku#Enumerat...](https://en.wikipedia.org/wiki/Mathematics_of_Sudoku#Enumerating_essentially_different_Sudoku_solutions)

------
fsakura
Wow, and here I thought writing sudoku solver in Java is a tough task.

~~~
lvh
If you think Sudoku solvers are cool, check out (constraint) logic
programming, e.g.
[https://github.com/clojure/core.logic/wiki/Examples#sudoku](https://github.com/clojure/core.logic/wiki/Examples#sudoku)

You can take that pretty far! I used it to solve Regex Crossword [0]:
[https://www.lvh.io/posts/solving-regex-
crosswords/](https://www.lvh.io/posts/solving-regex-crosswords/)

[https://regexcrossword.com/](https://regexcrossword.com/)

------
gfody
and a 3D cube:
[https://observablehq.com/@pallada-92/sql-3d-engine](https://observablehq.com/@pallada-92/sql-3d-engine)

