How a simple code can produce a Sierpinski triangle

Published on
Updated on

Sometimes even a simple code like this can produce an unbelievable output:

<pre style="text-align: center; min-width: 700px;"><script>
let arr = [];

for (let i = 0; i < 32; ++i) {
	arr = arr.map((_, i) => arr[i] !== arr[i - 1] ? '*' : ' ');
	arr.push('*');
	document.writeln(arr.join(' '));
}
</script></pre>

Output:


	
		*
		* *
		*   *
		* * * *
		*       *
		* *     * *
		*   *   *   *
		* * * * * * * *
		*               *
		* *             * *
		*   *           *   *
		* * * *         * * * *
		*       *       *       *
		* *     * *     * *     * *
		*   *   *   *   *   *   *   *
		* * * * * * * * * * * * * * * *
		*                               *
		* *                             * *
		*   *                           *   *
		* * * *                         * * * *
		*       *                       *       *
		* *     * *                     * *     * *
		*   *   *   *                   *   *   *   *
		* * * * * * * *                 * * * * * * * *
		*               *               *               *
		* *             * *             * *             * *
		*   *           *   *           *   *           *   *
		* * * *         * * * *         * * * *         * * * *
		*       *       *       *       *       *       *       *
		* *     * *     * *     * *     * *     * *     * *     * *
		*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *
		* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	

The code puts one * in the first row, after that in the next 31 rows puts * symbol only when it has exactly 1 adjacent * in the upper row, otherwise puts empty space ( ). Who could think that it can produce a recursive triangle, i.e. a fractal!





Read previous


UP
This site uses cookies. By continuing to use this website, you agree to their use. To find out more, including how to control cookies, see here: Privacy & cookies.