Conclave

As you all guys know India is surrounded by Pakistan, China, Nepal, Bhutan, Bangladesh and Burma by land. Among all neighbors Nepal and Bhutan are relatively neutral. But recently, cross border activities by rest of others is surging by a significant amount. Keeping in mind the current situation The Prime Minister of India chaired a conclave.

In this conclave only senators are allowed and all participants are assigned a particular unique number to keep their identity secret. But the assigned numbers are not arbitrary and it is within a specific number which is the number of participants in conclave. Since the post of Prime Minister is of highest importance, he is assigned number 1. The conclave is being held in a round table in which the position of Prime Minister is fixed.

Among all senators, one of them had some kind of belief that, if the sum of any adjacent numbers leads to a prime number then the things will go in right direction and conclave is a success. But when he started looking for such an arrangement he got stuck.

So now he is looking for someone’s help to find out such an arrangement. Can you help him to solve his problem?

Input

Input will be the number of participants in conclave (<10).

 

Output

Output will be the all possible arrangement for a particular number and the arrangement will start with Prime Minister’s number.

 

Sample Input

4
8

 

Sample Output

1234
1432

12385674
12583476
14765832
16743852

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

using System.IO;

using System.Text.RegularExpressions;

 

namespace Conclave

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                int numberOfParticipants = 8;

                //Create an array based on number of participants

                char[] charMatrix = new char[numberOfParticipants – 1];

                //Initializing character matrix

                for (int i = 0; i < numberOfParticipants – 1; i++)

                {

                    charMatrix[i] = char.Parse((i+2).ToString());

                }

                //Create the pointer array

                int[] pointer = new int[numberOfParticipants – 1];

                //Pointer array initialization

                for (int i = 0; i < numberOfParticipants – 1; i++)

                {

                    pointer[i] = i;

                }

                //Pointer increment loop

                pointer[numberOfParticipants – 2]–;

                do

                {

                    int i = numberOfParticipants – 2;

                label1:

                    pointer[i]++;

                    if (pointer[i] == numberOfParticipants – 1 && i != 0)

                    {

                        pointer[i] = 0;

                        i–;

                        goto label1;

                    }

                    else if (pointer[0] == numberOfParticipants – 1)

                    {

                        break;

                    }

                    else

                    {

                        bool valid = true;

                        //Check for valid pointer values

                        int[] check = new int[numberOfParticipants – 1];

                        for (int j = 0; j < numberOfParticipants – 1; j++)

                        {

                            check[pointer[j]]++;

                            if (check[pointer[j]] > 1)

                            {

                                valid = false;

                                break;

                            }

                        }

                        if (valid)

                        {

                            string tempStr = "1";

                            for (int j = 0; j < numberOfParticipants – 1; j++)

                            {

                                tempStr = tempStr + charMatrix[pointer[j]].ToString();

                            }

                            if(Success(tempStr))

                            {

                                Console.WriteLine(tempStr);

                            }

                        }

                    }

                } while (pointer[0] != numberOfParticipants – 1);

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

            }

        }

        public static bool Success(string str)

        {

            bool flag = false;

            for (int i = 0; i < str.Length-1; i++)

            {

                int tempNum = int.Parse(str.Substring(i, 1));

                tempNum = tempNum + int.Parse(str.Substring(i + 1, 1));

                flag = IsPrime(tempNum);

                if (flag == false)

                {

                    break;

                }

            }

            if (flag == true)

            {

                int tempNum = int.Parse(str.Substring(0, 1));

                tempNum = tempNum + int.Parse(str.Substring(str.Length – 1, 1));

                flag = IsPrime(tempNum);

            }

            return flag;

        }

        public static bool IsPrime(int num)

        {

            if (num <= 1)

            {

                return false;

            }

            else if (num == 2)

            {

                return true;

            }

            else

            {

                bool flag = true;

                for (int i = 2; i < num / 2; i++)

                {

                    if (num % i == 0)

                    {

                        flag = false;

                        break;

                    }

                }

                return flag;

            }

        }

    }

}

Output
========================
12385674
12583476
14765832
16743852
Press any key to continue . . .

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