We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

# Functions and Fractals: Sierpinski triangles

# Functions and Fractals: Sierpinski triangles

#### Sort by

recency

#### |

#### 39 Discussions

#### |

Please Login in order to post a comment

I came up with a solution that updates a matrix like an image and prints the matrix as the output. Here is my solution in Ocaml

My recursion solution in Haskell:

This problem is about recursion and functional programming.

Let's discover recursion pattern. The first figure:

r = 1 (recursion level)

h = 4 (hight of the figure)

The second figure:

r = 2

h = 8

Each triangle with h=4 on the last picture has been submitted by the identical 3 other triangles with half hight (h'=h/2) and less level of recursion (r'=r-1) from the first picture.

Thus, recursion function looks like

where

and

The transpose(arr), rectangle(h), line(h) functions will help to solve this problem with no val or var :)

I seemed to have thought about it differently. I saw each triangle a square where each coordinate is colored 1 if (c<=r) and 0 if (c>r). If you add a modulo function over powers of two, you can then generate each succesively smaller pattern. If you multiply them all together you end up with a single function that you then can apply over the range of rows and columns, drop the odd rows, pretty this up into a string and then print it out.

haskell:

`

Bash