Re: simplifying ulam spiral code
- To: mathgroup at smc.vnet.net
- Subject: [mg56423] Re: [mg56394] simplifying ulam spiral code
- From: DrBob <drbob at bigfoot.com>
- Date: Sun, 24 Apr 2005 03:29:30 -0400 (EDT)
- References: <200504230516.BAA03185@smc.vnet.net>
- Reply-to: drbob at bigfoot.com
- Sender: owner-wri-mathgroup at wolfram.com
This is somewhat "functional" and agrees (I think) with the picture at the link (depending on where we start, which isn't actually revealed). Clear[side, ell] next = {{0, -1}, {1, 0}}; initial = {start = {0, 0}, {1, 0}, 1}; side[i_]@{corner_, direction_, count_} := {Nest[Sow[# + direction] &, corner, count], next.direction, count + i} ell@{corner_, direction_, count_} := side[1]@side[0]@{corner, direction, count} limit = Ceiling[(1 + Sqrt[1 + 4*20000])/2]; list = Pick[#, PrimeQ@Range@Length@#] & @Prepend[First@Last@Reap@Nest[ell, initial, limit], start]; ListPlot[list, PlotStyle -> PointSize[0.007], AspectRatio -> Automatic, Axes -> True]; The posted code gives {1,1} and {0,1} as the first two colored squares, which means the direction followed on the second side is {-1,0}, since {1,1}+{-1,0}=={0,1}. The picture at the link would have {0,1} as the direction taken on the second side. Possibly the posted code winds clockwise or plots x and y reversed, or something. Bobby On Sat, 23 Apr 2005 01:16:49 -0400 (EDT), zak <chocolatez at gmail.com> wrote: > hi > in the site: > http://teachers.crescentschool.org/weissworld/m3/spiral/spiral.html > there is a mathematica code for drawing ULAM'S SPIRAL > the code is: > > dat = Table[i, {i, 20000}]; > x := 0; y := 0; num := 1; shift := 1; > point := {x, y}; list = {} > While[num <= 20000, > i := 1; > While[i <= shift, > x = x + 1; > If[PrimeQ[dat[[num]]] == True, list = Flatten[{list, point}]]; num++; > i++;]; i := 1; While[i <= shift, > y = y + 1; > If[PrimeQ[dat[[num]]] == True, list = Flatten[{list, point}]]; num++; > i++;]; shift++; > i := 1; > While[i <= shift, > x = x - 1; > If[PrimeQ[dat[[num]]] == True, list = Flatten[{list, point}]]; num++; > i++;]; i := 1; > While[i <= shift, > y = y - 1; > If[PrimeQ[dat[[num]]] == True, list = Flatten[{list, point}]]; num++; > i++;]; shift++;] > ListPlot[Partition[list, 2], PlotStyle -> PointSize[0.007], > AspectRatio -> Automatic, Axes -> True]; > > > or see text in: > http://sr1.mytempdir.com/15902 > > > Please i hope someone transform it to a functional programming code. > thanks > zak > > > > -- DrBob at bigfoot.com
- References:
- simplifying ulam spiral code
- From: zak <chocolatez@gmail.com>
- simplifying ulam spiral code