Sunday, October 30, 2011

GCD (greatest common deviser) Consecutive integers and Uclide method


Algorithm to find GCD (greatest common deviser)
Consecutive integers and Uclide method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace algorithm
{
    class GCDalgos
    {
        int  t;
        int  r;
        int size;
        public GCDalgos() { }
        //computer gcd of m and n value by uclide method
        //input: two non-negative integers and non zero
        //output: the greates common diviser of m and n value

        public int uclide(int  _m,int size)
        {
            if (size > _m)
            {
            int temp = size;
            size = _m;
            _m = temp;
            }
            if (size == 0 || size ==1)
            { Console.WriteLine("the GCD By uclid is: {0}",_m);}
            else
            {
                r = _m % size;
                _m = size;
                size = r;
               
                uclide(_m, size);
               
            }
            return 0;
        }
        //computer gcd by consecutive integer method of m and n
        //input: two non-negative integers and non zero
        //output: the greates common diviser of m and n value
        public int consecutiveinteger(int m, int n)
        {
            //if (m == 0 || n == 0) { return m; }
            if (m < n)
            {
                t = m;
            }
            else
            {
                t = n;
            }
            while (t != 1)
            {
                if (m % t == 0)
                {
                    if (n % t == 0)
                    {
                        return t;
                    }
                    t = t - 1;
                }
                else t = t-1;
            }
            return m;
        } 
    }
    class Program
    {
        static void Main(string[] args)
        {
            GCDalgos  a = new GCDalgos();
            Console.WriteLine("Enter value of m");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter value of n");
            int m = Convert.ToInt32(Console.ReadLine());

            a.uclide(m, n);
            Console.WriteLine("The GCD by Consecutive integer is: {0}", a.consecutiveinteger(m, n));
            Console.Read();
        }
    }
}

Algorithm to find GCD (greatest common deviser) Primary school GCD method


Algorithm to find GCD (greatest common deviser)
Primary school GCD method
//computer gcd of m and n value by primary school method
//input: two non-negative integers and non zero and array m & n //of n size
//output: the greates common diviser of m and n value
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sieve
{
    class algo
    {
        int n,j;
        int[] arr;
        int[] l;
        int[] m;
        int[] _n;
        public algo() { }
        public algo(int _m)
        {
            n = _m + 1;
            arr = new int[n];
            l = new int[n];
            m = new int[n];
            _n = new int[n];
        }

        public void sievealgo()
        {
            for (int p = 2; p < n; p++)
            {
                arr[p] = p;
            }
            for (int p = 2; p < Math.Sqrt(n) ; p++)
            {
                if (arr[p] != 0)
                {
                    j = p * p;
                }
                while (j <= n-1)
                {
                    arr[j] = 0;
                    j += p;
                   
                }
            }
            int i = 0;
            for (int a = 0; a < n; a++)
            {
                if (arr[a] != 0)
                {
                    l[i] = arr[a];
                    i++;
                }
            }
            for (int p = 0; p < n; p++)
            {
                if (l[p] != 0)
                    Console.WriteLine(l[p]);
            }
        }
        public void primegcd(int _m,int nn)
        {
            int valuem = _m;
            int d=0,st=0;
            while (valuem != 1 && l[st] != 0)
            {
                if (valuem % l[st] == 0 )
                {
                    m[d] = l[st];
                    valuem = valuem / l[st];
                    st = 0;
                    d++;
                }
                else st++;
            }
            int valuenn = nn;
            int dd = 0, stt = 0;
            while (valuenn != 1 && l[stt] != 0)
            {
                if (valuenn % l[stt] == 0)
                {
                    _n[dd] = l[stt];
                    valuenn = valuenn / l[stt];
                    stt = 0;
                    dd++;
                }
                else stt++;
            }
            for (int p = 0; p < n; p++)
            {
                if(_n[p] != 0 || m[p]!=0)
                Console.WriteLine("m= {0}\t n= {1}",m[p],_n[p]);
            }
            int gcd=1;
            int i = 0;
            while(m[i] != 0)
            {
                for (int j = 0; j < n; j++)
                {
                    if (_n[j] == m[i] && m[i] != 0)
                    {
                        gcd =gcd * _n[j];
                        _n[j] = 0;
                        m[i] = 0;
                    }
                }
                i++;
            }
            Console.WriteLine("vale m= {0}\t value  n={1}", _m, nn);
            Console.WriteLine("gcd {0}", gcd);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            algo a = new algo(32);
            a.sievealgo();
            Console.WriteLine("seprator");
            a.primegcd(150,154);
        }
    }
}

Sunday, October 23, 2011

Count Sort Complete Code.

Count Sort Complete Code.

Counting Sort Program

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace COUNT_SORT
{
    class sorting
    {
        int n;
        int[] cs;
        int[] count;
        int[] s;
        public sorting() { }
        public sorting(int _n)
        {
            n = _n;
            cs = new int[n];
            count = new int[n];
            s = new int[n];
        }
        public void Countsort()
        {
            Console.WriteLine("Enter The value you want sort");
            for (int i = 0; i < n; i++)
            {
                cs[i] = Convert.ToInt32(Console.ReadLine());
            }
            for (int i = 0; i < n; i++)
            {
                count[i] = 0;
            }
            for (int i = 0; i < n - 1; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (cs[i] < cs[j])
                    { count[j]++; }
                    else { count[i]++; }
                }
            }
            for (int i = 0; i < n; i++)
            {
              
                for (int j = 0; j < n; j++)
                {
                    Console.Write(s[j]);
                }
                Console.WriteLine();
                s[count[i]] = cs[i];
            }
            for (int j = 0; j < n; j++)
            { Console.Write(s[j]); }
            Console.WriteLine();
            Console.ReadLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            sorting s = new sorting(10);
            s.Countsort();
        }
    }
}



Saturday, October 22, 2011

General Tree Data Structure.

General Tree Data Structure.
Each node at at right including root node is a parent node
& node that will be added at left will be the child node.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Genreal_tree_node
{
     class bst_node
    {
        public int id;
        public bst_node left, right;

        public bst_node()
        {
            id = 0;
            left = right = null;
        }
        public bst_node(int id1)
        {
            id = id1;
            left = right = null;
        }
    }
     class binarytree
     {
         bst_node root;
         bst_node parent;
         bst_node temp;
         public binarytree()
         { parent = temp = root = null; }
         public void inserhelper(int id)
         {
             insertnode(id, ref root);
         }
         private void insertnode(int id, ref bst_node r)
         {
             if (r == null)
                 r = new bst_node(id);
             else if (id < r.id)
                 insertnode(id, ref r.left);
             else if (id > r.id)
                 insertnode(id, ref  r.right);
             else Console.WriteLine("Same data not allowed");
         }
         public void inordertraversehelper()
         {
             inordertravers(root);
         }
         private void inordertravers(bst_node r)
         {
             if (r != null)
             {
                 inordertravers(r.left);
                 Console.WriteLine(r.id);
                 inordertravers(r.right);
             }
         }
         public void maximno()
         {
             bst_node temp = root;
             while (temp != null)
             {
                 temp = temp.right;
                 if (temp == null)
                 {
                     break;
                 }
             }
             Console.WriteLine(temp.id);
         }
         public void minimum()
         {
             bst_node temp = root;
             while (temp != null)
             {
                 temp = temp.left;
                 if (temp == null)
                 {
                     break;
                 }
             }
             Console.WriteLine(temp.id);
         }
          public void searchhelper(int a)
            {
              sesarch(a,ref root);
            }
          private  void  sesarch(int a, ref bst_node r)
          {
              if (r != null)
              {

                  if (a == r.id)
                  {
                      parent = r;       
                  }
                  sesarch(a, ref r.left);
                  sesarch(a, ref r.right);
              }
          }
          public void genrealtreeinsert()
          {
              int a,b;
              if (root == null)
              {
                  Console.WriteLine("Enter the value of parent\n");
                  a = Convert.ToInt32(Console.ReadLine());
                  root = new bst_node(a);
                  Console.Write("How much child root should have\n");
                  int c = Convert.ToInt32(Console.ReadLine());
                  Console.WriteLine("Enter the value of Child");
                  b = Convert.ToInt32(Console.ReadLine());
                  temp = root.left = new bst_node(b);
                  while (c != 1)
                  {
                      Console.WriteLine("Enter the value of Child\n");
                      b = Convert.ToInt32(Console.ReadLine());
                      temp.right = new bst_node(b);
                      temp = temp.right;
                      c--;
                  }
              }
              else
              {
                  childadder();
              }
             
          }
          private void childadder()
          {
              int a, b;
              Console.WriteLine("*****At which node do you want add child******");
              a = Convert.ToInt32(Console.ReadLine());
              sesarch(a, ref root);
              if (parent.left == null)
              {
                  Console.Write("How much child root should have");

                  int c = Convert.ToInt32(Console.ReadLine());
                  Console.WriteLine("Enter the value of Child");

                  b = Convert.ToInt32(Console.ReadLine());
                  temp = parent.left = new bst_node(b);
                  while (c != 1)
                  {
                      Console.WriteLine("Enter the value of Child");
                      b = Convert.ToInt32(Console.ReadLine());
                      temp.right = new bst_node(b);
                      temp = temp.right;
                      c--;
                  }

              }
              else
              {
                  Console.WriteLine("Node alredy contain child");
                  Console.WriteLine("DO YOU WANT OVERWRITE\nPress y to overwrite");
                  string check = Console.ReadLine();
                  if (check == "y" || check == "y")
                  {
                      parent.left = null;
                      childadder();
                  }
              }
          }
      
     }
    class Program
    {
        static void Main(string[] args)
        {
            binarytree tree = new binarytree();
            Console.WriteLine("-----------------");
            tree.genrealtreeinsert();
            tree.genrealtreeinsert();
            tree.genrealtreeinsert();
            Console.WriteLine("-----------------");
            //tree.searchhelper(4);
            Console.WriteLine("-----------------");
            tree.inordertraversehelper();

        }
    }
}

used in operatonal research LP(linear programming) The Simplex Algorithm Simplex method Resolve using the Simple...