The Spiral of Primes

Prime numbers (those that have only themselves and other primes as factors) starting with 2 can be arranged on a two dimensional plane starting as show below:

59 ¬ 53 ¬ 47 ¬ 43 ¬ 41

¯                                      ­

          11 ¬ 7 ¬ 5          37

           ¯              ­           ­

          13      2 ® 3          31

           ¯                            ­

          17 ® 19 ® 23 ® 29

Assume a Cartesian coordinate system is used to reference the primes, and that 2 is located at (0,0). We can then see that 3 is located at (1,0), 5 is located at (1,1), 7 is located at (0,1) and 11 is located at (–1,1). Given the coordinates of a prime number in this system, find and display the prime number at that location.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace PrimeSpiral

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                Console.Write("Enter x coordinate of spiral prime: ");

                int xc = int.Parse(Console.ReadLine().ToString());

                Console.Write("Enter y coordinate of spiral prime: ");

                int yc = int.Parse(Console.ReadLine().ToString());

                int x = 0, y = 0, counter = 1;

                for (int k = 0; xc != 0 || yc != 0; k++)

                {

                    //Right

                    for (int i = 0; i < (2 * k + 1); i++)

                    {

                        x++;

                        counter++;

                        if (x == xc && y == yc)

                        {

                            goto labelExit;

                        }

                    }

                    //Up

                    for (int i = 0; i < (2 * k + 1); i++)

                    {

                        y++;

                        counter++;

                        if (x == xc && y == yc)

                        {

                            goto labelExit;

                        }

                    }

                    //Left

                    for (int i = 0; i < (2 * k + 2); i++)

                    {

                        x–;

                        counter++;

                        if (x == xc && y == yc)

                        {

                            goto labelExit;

                        }

                    }

                    //Down

                    for (int i = 0; i < (2 * k + 2); i++)

                    {

                        y–;

                        counter++;

                        if (x == xc && y == yc)

                        {

                            goto labelExit;

                        }

                    }

                }

            labelExit:

                Console.WriteLine("Spiral Order of the Coordinates is: " + counter);

                Console.WriteLine("Prime number at location (" + xc + "," + yc + ") is " + Prime(counter));

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

            }

        }

        public static int Prime(int counter)

        {

            if (counter == 1)

            {

                return 2;

            }

            else if (counter > 1)

            {

                int count = 1;

                for (int i = 3; i < int.MaxValue; i++)

                {

                    int flag = 0;

                    for (int j = 2; j < i; j++)

                    {

                        if (i % j == 0)

                        {

                            flag = 1;

                            break;

                        }

                    }

                    if (flag == 0)

                    {

                        count++;

                        if (count == counter)

                        {

                            return (i);

                        }

                    }

                }

                return 0;

            }

            else

            {

                return 0;

            }

        }

    }

}

 

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s