Ulam Spiral From A Rectangular Spiral


 Product(s):OpenBuildings Generative Components
 Version(s):10.06.03.04
 Environment: N\A

 

Background

Prime numbers never fail to amaze us. One example of this would be the Ulam Spiral. If we have a rectangular spiral where each point represents a number. The starting point denotes 1 and the next point is 2 and so on so for as the spiral proceeds. We would find that some set of prime numbers line up in a series of straight lines. If we only display the points that represent the prime numbers we can notice an amazing pattern emerging out.

Steps to Accomplish

Step 1

        Create a list of Prime numbers. This is discussed in the wiki Prime Numbers Pattern.

Step 2

Create the required set of points for constructing the rectangular spiral. This is done using by function techniques of point node.
The GC script for the ByFunction technique of the point node as follows.

function (CoordinateSystem CS, int no_of_loops, int dist, var NumberSeries)
{
    Point pt;
    int a=1;
    int y_up;
    int x_left;
    int y_down;
    int x_right;
    pt= new Point(this);
    pt.ByCartesianCoordinates(CS, dist, 0, 0.0);            //Starting point
    for (int i = 1; i <= no_of_loops; ++i)
    {
        a=a+1;
        if (NumberSeries.Contains(a))
        {
            pt= new Point(this);
            pt.ByCartesianCoordinates(CS, (i+1)*dist, (-i+1)*dist, 0.0);
        }
               
        
        for (int j = 2; j <= 2*i; ++j)         //Positive y Up
        {
            a=a+1;
            y_up=(-i*dist)+j*dist;
            if (NumberSeries.Contains(a))
            {
                pt= new Point(this);
            
                pt.ByCartesianCoordinates(CS, (i+1)*dist, y_up, 0.0);
            }
            
        }
        for (int k = 1; k <= 2*i; ++k)         //Positive x left
        {
            a=a+1;
            x_left=(i+1)*dist-k*dist;
            if (NumberSeries.Contains(a))
            {
                pt= new Point(this);
            
                pt.ByCartesianCoordinates(CS, x_left, y_up, 0.0);
            }
            
        }
        for (int l = 1; l <= 2*i; ++l)         //Positive y down
        {
            a=a+1;
            y_down=y_up-l*dist;
            if (NumberSeries.Contains(a))
            {
                pt= new Point(this);
            
                pt.ByCartesianCoordinates(CS, x_left, y_down, 0.0); 
            }
            
        }
        for (int m = 1; m <= 2*i; ++m)         //Positive y down
        {
            a=a+1;
            x_right=x_left+m*dist;
            if (NumberSeries.Contains(a))
            {
                pt= new Point(this);
            
                pt.ByCartesianCoordinates(CS, x_right, y_down, 0.0);
            }
            
        }
    }
    
}


         Please go through the sample file communities.bentley.com/.../Rectangular-Spiral.gct

Step 3

Once we have the point set for the rectangular spiral, we can further modify its script to only display the points that represent prime numbers. This could be done with an if statement which will check a condition before creating each point. Here the condition would be to check before creating the nth point if n exists in the previously created prime number list. In this way, we can get the Ulam's spiral from the rectangular spiral.
The GC script for the ByFunction technique of the point node as follows.

function (CoordinateSystem CS, int no_of_loops, int dist, var prime)
{
    Point pt;
    int a=1;
    int y_up;
    int x_left;
    int y_down;
    int x_right;
    for (int i = 1; i <= no_of_loops; ++i)
    {
        a=a+1;
        if (prime.Contains(a))
        {
            pt= new Point(this);
            pt.ByCartesianCoordinates(CS, (i+1)*dist, (-i+1)*dist, 0.0);
        }
        
        for (int j = 2; j <= 2*i; ++j)         //y-direction Up
        {
            a=a+1;
            y_up=(-i*dist)+j*dist;
            if (prime.Contains(a))
            {
                pt= new Point(this);
            
                pt.ByCartesianCoordinates(CS, (i+1)*dist, y_up, 0.0);
            }
            
        }
        for (int k = 1; k <= 2*i; ++k)         //x-direction left
        {
            a=a+1;
            x_left=(i+1)*dist-k*dist;
            if (prime.Contains(a))
            {
                pt= new Point(this);
            
                pt.ByCartesianCoordinates(CS, x_left, y_up, 0.0);
            }
            
        }
        for (int l = 1; l <= 2*i; ++l)         // y-direction down
        {
            a=a+1;
            y_down=y_up-l*dist;
            if (prime.Contains(a))
            {
                pt= new Point(this);
            
                pt.ByCartesianCoordinates(CS, x_left, y_down, 0.0); 
            }
            
        }
        for (int m = 1; m <= 2*i; ++m)         // x-direction right
        {
            a=a+1;
            x_right=x_left+m*dist;
            if (prime.Contains(a))
            {
                pt= new Point(this);
            
                pt.ByCartesianCoordinates(CS, x_right, y_down, 0.0);
            }
            
        }
    }
    
}

Ulam's Spiral of 669 Prime Numbers 
                Ulam's Spiral of 669 Prime Numbers

669 Random Points
                  Just 669 Random Points

Please go through the sample file communities.bentley.com/.../Ulam_2700_s-Spiral.gct