# Creating Ulam Spirals with Origin C – Part II

posted in: Graphing, Programming | 0

In a previous blog post we covered how to generate the basemap of a Ulam spiral by using a C++ script in Code Builder. In this post we’ll be appending that script with some basic functions allowing you to display prime numbers, odd numbers, and numbers divisible by specific integers. We’ll then plot the resulting matrices and show you how to create a basic GUI for calling these different functions. You can view the end result of this blog by downloading the .cpp and .opj files here.

With the way this script has been structured it is very easy for us to add additional functions that rely on our CSpiral function but can be added without modifying it.

Between the //CSpiral and //UlamSpiral sections of our basic script, add the following

Then we’ll need to update the UlamSpiral function to be able to call this script as one of our options; for this reason the end of our script will now appear as such, note the comments in CAPS-

Once this updated script has been built, create a new matrix or open an existing one. Then in the main menu go to Matrix>Set Matrix Dimension/Labels…, and set its dimensions to 100×100 or 200×200. This will allow us to better view the frequency of prime numbers in our spiral. Next open the Script Window and type “UlamSpiral(2)”. If the script works you should see that it generated a spiral that displays only those numbers which are primes, whereas those that are not are displayed as missing numbers (–).

Now that we have our prime values, let’s plot them. Select the whole matrix, and go to Plot>Contour/Heat Map>Heat Map. At first you should see a blank plot with “Speed Mode is On” emblazoned across the graph. To change this, simply click on the little running man icon that looks like this  to turn off Speed Mode. By default this button should appear on the upper right-hand side of your workspace, next to the rescale button. Your graph should now appear like so-

As an aside–
While our matrix is 200 by 200 cells in size, it’s important to note that the size of the matrix represents an index that is not directly tied to the x and y values of the graph. If you go back into the matrix and then in the main menu select “Matrix>Set Matrix Dimension/Labels…” you’ll see that in columns are mapped for x values from 1 to 10 by default. There is however no reason to change the mapping values as the axes are meaningless in this context anyway.

Next select and delete the axes, scale, titles, and color scale if you so choose. Since our series is being displayed as a spiral, these numbers bear no meaning. To make our graph more legible, double click on the heat map to open the Plot Details dialog and then in the Colormap/Contours tab change the Missing Values color from the default to either White or None. After clicking okay, you may want to change the color palette to something that contrasts with the white background. For the purposes of our example, I would recommend selecting the “Blue”, “Green” or “Red” palettes that appear in Origin 2016’s new palette menu.

Now that we can visualize our spiral, let’s add one last feature to our script. Say we want to design a basic input function, allowing users to view patterns generated by different denominators. In this case our function script, added between //CPrimeSpiral and the //UlamSpiral calling function, will look very simple-

Our UlamSpiral calling function will then be updated to place another line in our “enum” section, like so-