[squeak-dev] Ulam spiral

H. Hirzel hannes.hirzel at gmail.com
Wed Oct 3 08:24:34 UTC 2012


This is very nice indeed.

What about adding this as an example of good code to the help menu?

Just a workspace like the one 'extending the system'. It would contain
the code to rebuild the background from scratch.  The menu might be
called 'Create Ulam spiral background'. The  would be an adapted
version of Bert's code.
1) It is short an easy to understand
2) The speed is just fine to watch it progressing.


And maybe  Franks solution in addition to illustrate another point.

Bert's code

 Display restoreAfter: [
                size := 400 squared.
                primes := (Integer primesUpTo: size) asSet.
                step := 1.
                length := 1.
                pen := Pen new turn: 90.
                [step < size] whileTrue: [
                        2 timesRepeat: [
                                length timesRepeat: [
                                        pen fillColor: ((primes includes: step)
                                                ifTrue: [Color black]
                                                ifFalse: [Color white]).
                                        pen go: 1.
                                        step := step + 1].
                                pen turn: -90].
                        length := length + 1]].


It should be adapted to do the following:

1) set the background to blank
2) build the spiral

--Hannes

On 10/1/12, Frank Shearar <frank.shearar at gmail.com> wrote:
> On 1 October 2012 16:55, Bert Freudenberg <bert at freudenbergs.de> wrote:
>> Frank posted some nice code on his blog:
>> http://www.lshift.net/blog/2012/09/27/decomposing-the-ulam-spiral
>>
>> Couldn't help myself coming up with a simpler (admittedly less efficient)
>> version:
>>
>>         Display restoreAfter: [
>>                 size := 400 squared.
>>                 primes := (Integer primesUpTo: size) asSet.
>>                 step := 1.
>>                 length := 1.
>>                 pen := Pen new turn: 90.
>>                 [step < size] whileTrue: [
>>                         2 timesRepeat: [
>>                                 length timesRepeat: [
>>                                         pen fillColor: ((primes includes:
>> step)
>>                                                 ifTrue: [Color black]
>>                                                 ifFalse: [Color white]).
>>                                         pen go: 1.
>>                                         step := step + 1].
>>                                 pen turn: -90].
>>                         length := length + 1]].
>
> I bow before your image-fu. The main thing I wanted to show was the
> decomposition of the problem into various bits. Also, I hadn't
> considered the idea of wrapping up the number line before, so needed
> to share that tiny moment of mathematical insight.
>
> For origin-centred parts of the Ulam spiral, you might well have
> something more efficient. Splitting up the problem into a (random
> access!) traversal only comes into its own when you can't use the
> intuition of a turtle going round and round in a square spiral. (But I
> did use that intuition in figuring out the offsets from the corners.)
>
> frank
>
>> - Bert -
>>
>>
>>
>
>


More information about the Squeak-dev mailing list