# Creating Ulam Spirals with Origin C – Part I

posted in: Programming | 0

I would like to thank my colleagues Eric Parent & Zheng Shao for writing the Origin C scripts that made this post possible.

In the middle of a long scientific presentation in 1963, renowned mathematician and inventor Stanislaw Ulam (pronounced oo-lam) was doodling on a pad of paper and began listing integers starting with 1 in a counterclockwise spiral. Like so-

As he drew this spiral he began circling the prime numbers, only to notice this-

There seemed to be “lines” of prime numbers going outward in a distinct pattern. Primes which, by definition, are indivisible by any number but 1 and themselves, may seem to occur at random intervals at first, but there is in fact a certain logic behind this chaos, as we can see in the grid below.

Ulam spirals are largely mathematical art, however they have shown promise for illustrating not only the general behaviors of various number series, but also an older mathematic conjecture that may explain the frequency of prime numbers. In this blog, I’ll be showing you how a matrix of any size can be combined with a C script in Origin to generate a Ulam spiral. This spiral can be used to illustrate primes as well as any other number series expressed in OriginC.

Let’s begin by opening up the Code Builder and creating a new C++ file, which can be saved in either your User Files (by default) or anywhere you have user permissions.

One of our initial challenges is finding a way to place 1 in the center of a matrix of equal width and height, and count outward until it reaches the value of the total number of cells in the entire grid- regardless of the matrix’s size. Pasting the following code into our C++ file will generate this spiral. To understand what each section does, you can follow along with the //comments in the script below.

This code generates the spiral, however we need a way to call this as a function from Origin’s script window. To do this, in Code Builder we’ll add the following lines to the end of our script-

To use this in Origin, the next thing we need to do is right-click on the User [Autoload] file or another “User files” folder in the Codebuilder workspace window  and select “Add Files…”. Locate the .cpp file in its folder, and click okay. Then select your file in the workspace, and go to Build>Build (F8). This will compile your code and should generate a working function that can be executed from Origin’s script window.

To test this function create a new matrix window, and then open the Script Window. In the script window type UlamSpiral(1), and you should see that the center cell is populated with 1 with values increasing by with each cell in a counter-clockwise order. If this didn’t work you may want to either copy/paste the script above or go back to the original file included in this post’s ZIP folder.

This is how you would generate the base values for a Ulam Spiral. In another post, we’ll be covering how to set it to display only primes as well as odd/even numbers or numbers divisible by different integers.