Post Reply 
Ulam spiral
12-22-2013, 09:15 PM (This post was last modified: 12-22-2013 09:48 PM by Tugdual.)
Post: #1
Ulam spiral
Explanation is here http://en.wikipedia.org/wiki/Ulam_spiral
The code is nothing special...

Code:

EXPORT Ulam()
BEGIN
   local x, y, level, i, n;
   x:=320/2;
   y:=240/2;
   level:=1;
   n:=1;

   rect_p(0);

   // center: initial state, level 1
   if isprime(n) then pixon_p(x,y); end;
   
   while x>=0 and x<320 and y>=0 and y<240 do
      // end of level -> move right
      x:=x+1;
      n:=n+1;
      level:=level+2;
      if isprime(n) then pixon_p(x,y,#FFFFFFh); end;

      // move up
      for i from 3 to level do
         y:=y-1;
         n:=n+1;
         if isprime(n) then pixon_p(x,y,#FFFFFFh); end;
      end;
      
      // move left
      for i from 2 to level do
         x:=x-1;
         n:=n+1;
         if isprime(n) then pixon_p(x,y,#FFFFFFh); end;
      end;

      // move down
      for i from 2 to level do
         y:=y+1;
         n:=n+1;
         if isprime(n) then pixon_p(x,y,#FFFFFFh); end;
      end;

      // move right
      for i from 2 to level do
         x:=x+1;
         n:=n+1;
         if isprime(n) then pixon_p(x,y,#FFFFFFh); end;
      end;   
   end;

freeze;
END;


And here is the Sacks version (note: why do I have to use approx???)
Code:

EXPORT Sacks()
BEGIN
local r, a, n, x, y;
rect_p(0);
n:=0;
   while x>=0 and x<320 and y>=0 and y<240 do
      if isprime(n) then
         r:=√n;
         a:=r*2*π;
         x:=approx(r*cos(a)+160);
         y:=approx(120-r*sin(a));
         pixon_p(x,y,#FFFFFF);
      end;
      n:=n+1;
   end;
freeze;
END;
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Ulam spiral - Tugdual - 12-22-2013 09:15 PM
RE: Ulam spiral - Damien - 12-25-2013, 07:09 PM
RE: Ulam spiral - logoliv - 08-28-2020, 11:09 AM
RE: Ulam spiral - Joe Horn - 08-28-2020, 01:14 PM
RE: Ulam spiral - logoliv - 08-28-2020, 02:33 PM



User(s) browsing this thread: 1 Guest(s)