Archive for the 'Algorithms' Category

Walk a grid

 This algorthm walks a two-dimensional array (eg. a grid)  row by row.


            //Grid
            int height = 10;
            int width = 10;
            char[,] array = new char[width, height];

            //Position
            int y = 1; //y position
            int x = 1; //x position   

            while (y <= height)
            {
                while (x <= width)
                {
                    array[y - 1, x - 1] = item; //Assign
                    x++;
                }
                y++;
                x = 1;
            }

Finding Prime Numbers

Here’s my prime number algorithm implementation that I have written in C#.


        public static bool IsPrimeNumber(double x)
        {
            if (x <= 1)
            {
                return false;
            }
            else if (!(x % 1 != 0))
            {
                double i = 2;

                bool r = true;
                while (i <= Math.Sqrt(x))
                {
                    if (!((x / i) % 1 != 0))
                    {
                        r = false;
                        break;
                    }
                    i++;
                }
                return r;
            }
            else
            {
                return false;
            }
        } 


Pages

Categories