Last active
August 26, 2025 16:12
-
-
Save simonw/14cab44a5a169806263bcb8e481944aa to your computer and use it in GitHub Desktop.
Mandelbrot SQLite query example from https://www.sqlite.org/lang_with.html#outlandish_recursive_query_examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Example from https://www.sqlite.org/lang_with.html#outlandish_recursive_query_examples | |
| CREATE TABLE mandelbrot AS WITH RECURSIVE | |
| xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+0.05 FROM xaxis WHERE x<1.2), | |
| yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+0.1 FROM yaxis WHERE y<1.0), | |
| m(iter, cx, cy, x, y) AS ( | |
| SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis | |
| UNION ALL | |
| SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m | |
| WHERE (x*x + y*y) < 4.0 AND iter<28 | |
| ), | |
| m2(iter, cx, cy) AS ( | |
| SELECT max(iter), cx, cy FROM m GROUP BY cx, cy | |
| ), | |
| a(t) AS ( | |
| SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') | |
| FROM m2 GROUP BY cy | |
| ) | |
| SELECT group_concat(rtrim(t),x'0a') FROM a; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo in Datasette Lite: https://lite.datasette.io/?sql=https%3A%2F%2Fgist.githubusercontent.com%2Fsimonw%2F14cab44a5a169806263bcb8e481944aa%2Fraw%2F88b237ba019e5330fcb6f66ce0c8f626a11df097%2Fmandelbrot.sql#/data/mandelbrot