C#: Project Euler Solution to Problem 35

Project Euler Problem #35

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

static void Main(string[] args)
        {
            DateTime d1 = DateTime.Now;
            LinkedList<int> unique_CirclePrime = new LinkedList<int>();
            LinkedList<int> tempList = new LinkedList<int>();
            int currentPrime = 1;
            while (currentPrime <= 1000000)
            {
                string initNum2Rotate = currentPrime.ToString();
                int c = 0;
                int placeHolder = 0;
                for (int j = 0; j < initNum2Rotate.Length; j++)
                {
                    int previousdisplay = 0;
                    string displayString = "";
                    for (int i = 0; i < initNum2Rotate.Length; i++)
                    {
                        if (placeHolder == initNum2Rotate.Length)
                            placeHolder = 0;
                        int display = (placeHolder + c);
                        if (display == initNum2Rotate.Length)
                            display = 0;
 
                        if (display > initNum2Rotate.Length)
                            display = previousdisplay + 1;placeHolder++;
                        string gluestring = initNum2Rotate[display].ToString();
                        previousdisplay = display;
                        displayString += gluestring;
                    }
                    c++;
                    tempList.AddLast( int.Parse(displayString) );
                }
                bool allprimes = false;
                foreach (int item in tempList)
                {
                    if (isPrime(item))
                        allprimes = true;
                    else
                    {
                        allprimes = false;
                        break;
                    }
                }
                if (allprimes)
                {
                    foreach (int item in tempList)
                    {
                        if (!unique_CirclePrime.Contains(item))
                            unique_CirclePrime.AddLast(item);
                    }
                }
                tempList.Clear();
                currentPrime++;
            }
 
            int counter = 1;
            foreach (var item in unique_CirclePrime)
            {
                Console.WriteLine(item + "       " + isPrime(item) + "       " + counter);
                counter++;
            }
            DateTime d2 = DateTime.Now;
 
            Console.WriteLine("Total Length of Unique Circular Primes : "+ unique_CirclePrime.Count);
            Console.WriteLine("time : " + (d2 - d1));
        }
 
        public static bool isPrime(int n)
        {
            if (n == 1)
                return false;
            if (n == 2)
                return true;
            for (int i = 2; i < n; ++i)
            {
                if ((n % i) == 0)
                    return false;
            }
            return true;
        }

One thought on “C#: Project Euler Solution to Problem 35

Leave a Reply

Your email address will not be published. Required fields are marked *