       Memory usage of a Sierpinski triangle algorithm

• To: mathgroup at smc.vnet.net
• Subject: [mg124128] Memory usage of a Sierpinski triangle algorithm
• From: Wojciech Morawiec <wmorawie at students.uni-mainz.de>
• Date: Tue, 10 Jan 2012 05:58:57 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com

```Cheers!

I began some Mathematica programming exercises recently and I wrote some simple algorithm to create a list of points that resembles a Sierpinski triangle when plotted with ListPlot. Basically you choose on of the three edges by random, create a point in the middle between {0.5,0.5} and the chosen edge and continue to create new points in the middle between a randomly chosen edge and the last created point:

########################### CODE ###########################
throwd3[] := Module[{}, x = Random[];
If[x < 1/3, y = 1, If[1/3 < x < 2/3, y = 2, y = 3]]; y]

startlist = {{0, 0}, {1, 0}, {0.5, 1}};

Sierp[N_, x0_: 0.5, y0_: 0.5] :=
Module[{nmax = N, n, P, list}, Subscript[P, 0] = {x0, y0}; n = 1;
Subscript[list, 0] = startlist;
Do[Evaluate [
Subscript[P, n] =
1/2 (startlist[[throwd3[]]] + Subscript[P, n - 1])];
Subscript[list, n] =
Append[Subscript[list, (n - 1)], Subscript[P, n]], {n, nmax}];
ListPlot[Subscript[list, nmax], PlotStyle -> PointSize[0.0001]]]
########################### END CODE ########################

The thing is that this code uses huge amounts of memory since it stores all the lists and points needed during the calculations, although these shouldn't be stored because I'm using a Module, right?
Obviously, I'm missing something here...