Ulam spiral

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Ulam spiral

Bert Freudenberg
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]].

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Chris Cunnington
On 12-10-01 11:55 AM, Bert Freudenberg wrote:

> 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 like that a lot. I found myself executing it over and over.

Chris

Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Frank Shearar-3
In reply to this post by Bert Freudenberg
On 1 October 2012 16:55, Bert Freudenberg <[hidden email]> 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 -
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Hannes Hirzel
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 <[hidden email]> wrote:

> On 1 October 2012 16:55, Bert Freudenberg <[hidden email]> 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 -
>>
>>
>>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Frank Shearar-3
On 3 October 2012 09:24, H. Hirzel <[hidden email]> wrote:
> 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.

I guess it's as good a time as any to state that the background of 4.4
_will_ be some variation of the Ulam spiral. If you take a standard
gritty one and gently fuzz it, you get a light grey slightly textured
background that will tile nicely. But I was going to turn the mutated
spiral into a PNG and figure out how to set the background at some
point in the very near future.

> 1) It is short an easy to understand
> 2) The speed is just fine to watch it progressing.

+1. It also has the same kind of hook as a Logo program.

frank

> 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 <[hidden email]> wrote:
>> On 1 October 2012 16:55, Bert Freudenberg <[hidden email]> 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 -
>>>
>>>
>>>
>>
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Hannes Hirzel
On 10/3/12, Frank Shearar <[hidden email]> wrote:

> On 3 October 2012 09:24, H. Hirzel <[hidden email]> wrote:
>> 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.
>
> I guess it's as good a time as any to state that the background of 4.4
> _will_ be some variation of the Ulam spiral. If you take a standard
> gritty one and gently fuzz it, you get a light grey slightly textured
> background that will tile nicely. But I was going to turn the mutated
> spiral into a PNG and figure out how to set the background at some
> point in the very near future.
>
>> 1) It is short an easy to understand
>> 2) The speed is just fine to watch it progressing.
>
> +1. It also has the same kind of look as a Logo program.
>
> frank

Yes, I can imagine that some people might want to download a Squeak
4.4 all-in-one just for the sake of playing around with this spiral. A
good class-room demonstration of Squeak (for 20...40 minutes).

Things which it illustrates are
1) The compactness of the Smalltalk language
2) How to modify code in a Workspace and execute it with 'do it'.
3) The fact that you do not need to have 'deep' knowledge of Smalltalk
in order to adapt a piece of code.
4) It is like a 'poem'. A short self contained nice piece of code.


--Hannes

>> 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 <[hidden email]> wrote:
>>> On 1 October 2012 16:55, Bert Freudenberg <[hidden email]> 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 -
>>>>
>>>>
>>>>
>>>
>>>
>>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Jecel Assumpcao Jr
Hannes. Hirzel wrote on Wed, 3 Oct 2012 14:45:45 +0200
> Things which it illustrates are
> 1) The compactness of the Smalltalk language
> 2) How to modify code in a Workspace and execute it with 'do it'.
> 3) The fact that you do not need to have 'deep' knowledge of Smalltalk
> in order to adapt a piece of code.
> 4) It is like a 'poem'. A short self contained nice piece of code.

5) Code can be executed directly in the mail window. Oh wait, that is
just me - the last Celeste user ;-)

-- Jecel


Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Hannes Hirzel
In reply to this post by Hannes Hirzel
So the question is, do we get the Ulam spiral creation code snippet
included in Squeak 4.4?

--Hannes

On 10/3/12, Jecel Assumpcao Jr. <[hidden email]> wrote:

> Hannes. Hirzel wrote on Wed, 3 Oct 2012 14:45:45 +0200
>> Things which it illustrates are
>> 1) The compactness of the Smalltalk language
>> 2) How to modify code in a Workspace and execute it with 'do it'.
>> 3) The fact that you do not need to have 'deep' knowledge of Smalltalk
>> in order to adapt a piece of code.
>> 4) It is like a 'poem'. A short self contained nice piece of code.
>
> 5) Code can be executed directly in the mail window. Oh wait, that is
> just me - the last Celeste user ;-)
>
> -- Jecel
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Frank Shearar-3
On 12 October 2012 12:48, H. Hirzel <[hidden email]> wrote:
> So the question is, do we get the Ulam spiral creation code snippet
> included in Squeak 4.4?

You ask 4.4's release manager, who says "yes! and I'd like some
covering text too please!"

frank

> --Hannes
>
> On 10/3/12, Jecel Assumpcao Jr. <[hidden email]> wrote:
>> Hannes. Hirzel wrote on Wed, 3 Oct 2012 14:45:45 +0200
>>> Things which it illustrates are
>>> 1) The compactness of the Smalltalk language
>>> 2) How to modify code in a Workspace and execute it with 'do it'.
>>> 3) The fact that you do not need to have 'deep' knowledge of Smalltalk
>>> in order to adapt a piece of code.
>>> 4) It is like a 'poem'. A short self contained nice piece of code.
>>
>> 5) Code can be executed directly in the mail window. Oh wait, that is
>> just me - the last Celeste user ;-)
>>
>> -- Jecel
>>
>>
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Hannes Hirzel
I propose to add something like the following to the 'welcome workspace'.

 At the bottom, 'call to action  :-)  ,

cf Squeak web site discussion thread.

--Hannes
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The desktop background shows an prime spiral. Natural numbers are
arranged in a spiral in a rectangular grid. The 1 is at the center.
Primes are marked with a dot.

To see how this is done in Squeak Smalltalk
a) select the following code,
b) bring up the context menu and
c) choose 'do it'.

   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]].

Ref:  http://en.wikipedia.org/wiki/Ulam_spiral

On 12/5/12, Frank Shearar <[hidden email]> wrote:

> On 12 October 2012 12:48, H. Hirzel <[hidden email]> wrote:
>> So the question is, do we get the Ulam spiral creation code snippet
>> included in Squeak 4.4?
>
> You ask 4.4's release manager, who says "yes! and I'd like some
> covering text too please!"
>
> frank
>
>> --Hannes
>>
>> On 10/3/12, Jecel Assumpcao Jr. <[hidden email]> wrote:
>>> Hannes. Hirzel wrote on Wed, 3 Oct 2012 14:45:45 +0200
>>>> Things which it illustrates are
>>>> 1) The compactness of the Smalltalk language
>>>> 2) How to modify code in a Workspace and execute it with 'do it'.
>>>> 3) The fact that you do not need to have 'deep' knowledge of Smalltalk
>>>> in order to adapt a piece of code.
>>>> 4) It is like a 'poem'. A short self contained nice piece of code.
>>>
>>> 5) Code can be executed directly in the mail window. Oh wait, that is
>>> just me - the last Celeste user ;-)
>>>
>>> -- Jecel
>>>
>>>
>>>
>>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Bert Freudenberg

On 2012-12-07, at 00:03, "H. Hirzel" <[hidden email]> wrote:

> I propose to add something like the following to the 'welcome workspace'.
>
> At the bottom, 'call to action  :-)  ,
>
> cf Squeak web site discussion thread.
>
> --Hannes
> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> The desktop background shows an prime spiral. Natural numbers are
> arranged in a spiral in a rectangular grid. The 1 is at the center.
> Primes are marked with a dot.
>
> To see how this is done in Squeak Smalltalk
> a) select the following code,
> b) bring up the context menu and
> c) choose 'do it'.
>
>   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]].
>
> Ref:  http://en.wikipedia.org/wiki/Ulam_spiral



This version would be even less complex and one line shorter. But maybe a bit too sneaky? OTOH it demos fractional arithmetic, not a bad idea either:

Display restoreAfter: [
        size := 400 squared.
        primes := (Integer primesUpTo: size) asSet.
        step := 1.
        length := 1.
        pen := Pen new turn: 90.
        [step < size] whileTrue: [
                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/2)]].

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: Ulam spiral

Darius Clarke
Sounds good. :)