This website is still under construction, Last update 12/31/2023

Projects


Prime Spiral

There once was a very slow day at work, at a restaurant I used to work at. One of those days where you clean everything about 4 or 5 times, before coming to the stunning realization that there is literally nothing to do. So I began to doodle on a note book. I started writing all the positive integers in order, but in a square spiral around the center. I quickly filled the page and then decided to start circling the prime numbers.


2, 3, 5, 7, 13, 17, 19, 23 ... When I had finished, I realized that there was a strange looking pattern where along diagonals primes would create lines where there were primes, there were some gaps but I found this incredible interesting. It had peaked my curiosity, did I discover a new pattern? but obviously filling once sheet of paper was not enough to see a true pattern. I needed more! I later found out that I was not the first person to find this. Unfortunately for me a mathematician Stanisław Ulam discovered this first. But at the time I had now idea. Filled with curiosity I knew what I had to do.


I needed to write a program, to make the spiral for me! Below is the code I original wrote to do this. Keep in mind this was probably some time in 2017 or 2018


private void button1_Click(object sender, EventArgs e)
{
    int permutations = 0;
    int l=1000;
    int.TryParse(textBox1.Text, out l);
    permutations = l * l;
    x = (int)l / 2;
    y = (int)l / 2;
    r = 1;
    s = 0;
    pic = new Bitmap(l, l);
    for (int j = 0; j < pic.Width; j++)
    {
        for (int k = 0; k < pic.Height; k++)
        {
            pic.SetPixel(j, k, Color.Black);
        }
    }
    pictureBox1.Image = pic;
    try
    {
        for (int I = 0; I < permutations;)
        {
            if (r == 1)
            {
                //
                for (long ss = 0; ss < s; ss++)
                {

                    if (isPrime(i))
                    {
                        pic.SetPixel(x, y, Color.Aqua);
                    }
                    y += 1;
                    i++;
                }
                s++;
            }
            else if (r == 2)
            {
                for (int ss = 0; ss < s; ss++)
                {
                    if (isPrime(i))
                    {
                        pic.SetPixel(x, y, Color.Aqua);
                    }
                    x += 1;
                    i++;


                }
            }
            else if (r == 3)
            {
                for (int ss = 0; ss < s; ss++)
                {

                    if (isPrime(i))
                    {
                        pic.SetPixel(x, y, Color.Aqua);
                    }
                    y -= 1;
                    i++;
                }
                s++;
            }
            else if (r == 4)
            {
                for (int ss = 0; ss < s; ss++)
                {

                    if (isPrime(i))
                    {
                        pic.SetPixel(x, y, Color.Aqua);
                    }
                    x -= 1;
                    i++;
                }
                r = 0;
            }
            r++;
        }
    }
    catch(Exception ex)
    {
        pictureBox1.Image = pic;
        MessageBox.Show(ex.ToString());
    }
    pictureBox1.Image = pic;
    MessageBox.Show("done");
}
            

I was so exited, running the program and seeing that the pattern I thought I saw in my time of boredom. Continued to expand when expanded. The original algorithm to check whether a number was prime or not was pretty slow, so I began to try and think how I could make it faster. the first thing I came up with was that every other number is divisible by 2, so using the modulus operator I was able to check if it was divisible by 2 and then if it wasn't then I would essentially try to factor it. But this was still pretty slow, and I wanted more!

Its funny to think that my obsession with creating efficient and faster code, had its roots so far back. Something I only realize now as I'm writing this. I then turned to the Internet, and found another change I could make, there was no need to check every number between 3 and the number you are trying to figure out if its prime. Instead you can set a boundary of the square root of the number you are checking. This gave me the speed I desired and its how I created the image below, witch contains 100 million pixels. I would generate more but the limitations of the .png format force me to make it small. feel free to explore the primes a while.