Ejemplo n.º 1
0
        // BigNumbers is a class created in task 8 and added
        // possibility to multyplay BigNumbers
        static void Main()
        {
            // Accumulate n! starting from 2
            BigNumbers factorial = new BigNumbers(new byte[] { 2 });

            Console.Write("Input a number to calculate n!: ");
            int n = int.Parse(Console.ReadLine());

            for (int i = 3; i <= n; i++)
            {
                // Store a number into byte[]
                string str = i.ToString();
                byte[] val = new byte[str.Length];

                // Convert number to byte[]
                for (int ii = 0; ii < str.Length; ii++)
                {
                    val[ii] = (byte)(str[ii] - '0');
                }

                // Calculate BigNumbers factorial
                factorial *= new BigNumbers(val);
            }

            // Print the result
            Console.Write("The factorial {0}! = {1}", n, factorial);
            Console.WriteLine();
        }
Ejemplo n.º 2
0
        // Create operator * to multiplay tow big numbers
        public static BigNumbers operator *(BigNumbers num1, BigNumbers num2)
        {
            // Always longer number is stored in num1
            int len = GetLength(ref num1, ref num2);

            BigNumbers val = new BigNumbers(new byte[] { 0 });

            for (int i = 0; i < num2.bigNumber.Length; i++)
            {
                // Accumulate curent value in result list
                List <byte> res = new List <byte>();

                // Store the remainder
                int remainder = 0;

                for (int j = 0; j < len; j++)
                {
                    remainder += num1.bigNumber[j] * num2.bigNumber[i];

                    // Add curent digith to the list
                    res.Add((byte)(remainder % 10));

                    // Calculate the remainder
                    remainder = (remainder - (remainder % 10)) / 10;
                }

                // Add remainder
                if (remainder != 0)
                {
                    res.Add((byte)remainder);
                }

                res.Reverse();

                for (int zeros = 0; zeros < i; zeros++)
                {
                    res.Add(0);
                }


                val += new BigNumbers(res.ToArray());
            }

            // Return the result of multyplay
            return(val);
        }
Ejemplo n.º 3
0
        // Create operator * to multiplay tow big numbers
        public static BigNumbers operator *(BigNumbers num1, BigNumbers num2)
        {
            // Always longer number is stored in num1
            int len = GetLength(ref num1, ref num2);

            BigNumbers val = new BigNumbers(new byte[] { 0 });

            for (int i = 0; i < num2.bigNumber.Length; i++)
            {
                // Accumulate curent value in result list
                List<byte> res = new List<byte>();

                // Store the remainder
                int remainder = 0;

                for (int j = 0; j < len; j++)
                {
                    remainder += num1.bigNumber[j] * num2.bigNumber[i];

                    // Add curent digith to the list
                    res.Add((byte)(remainder % 10));

                    // Calculate the remainder
                    remainder = (remainder - (remainder % 10)) / 10;
                }

                // Add remainder
                if (remainder != 0)
                {
                    res.Add((byte)remainder);
                }

                res.Reverse();

                for (int zeros = 0; zeros < i; zeros++)
                {
                    res.Add(0);
                }

                val += new BigNumbers(res.ToArray());
            }

            // Return the result of multyplay
            return val;
        }
Ejemplo n.º 4
0
        // Always longer number is stored in num1
        private static int GetLength(ref BigNumbers num1, ref BigNumbers num2)
        {
            int len;

            if (num1.bigNumber.Length >= num2.bigNumber.Length)
            {
                // Length is the maximal length
                len = num1.bigNumber.Length;
            }
            else
            {
                // if num2 length > num1 length then swap num1 and num2
                len = num2.bigNumber.Length;
                BigNumbers tmp = num1;
                num1 = num2;
                num2 = tmp;
            }
            return(len);
        }
Ejemplo n.º 5
0
        static void Main()
        {
            // Create 3 BigNumber
            // This number could be verry big
            BigNumbers num1 = new BigNumbers(new byte[] { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 });
            // This one also
            BigNumbers num2 = new BigNumbers(new byte[] { 9, 9, 9, 9, 9 });
            BigNumbers num3 = num1 + num2;

            // Print BigNumber
            Console.WriteLine("BigNumber #1 = {0}", num1);
            Console.WriteLine("BigNumber #2 = {0}", num2);
            Console.WriteLine("BigNumber #3: (#1 + #2) = {0}", num3);

            // Sum
            num3  = num1;
            num3 += num1;

            // Print
            Console.WriteLine("BigNumber #3: (#1 + #1) = {0}", num3);
        }
Ejemplo n.º 6
0
        static void Main()
        {
            // Create 3 BigNumber
            // This number could be verry big
            BigNumbers num1 = new BigNumbers(new byte[] { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 });
            // This one also
            BigNumbers num2 = new BigNumbers(new byte[] { 9, 9, 9, 9, 9 });
            BigNumbers num3 = num1 + num2;

            // Print BigNumber
            Console.WriteLine("BigNumber #1 = {0}", num1);
            Console.WriteLine("BigNumber #2 = {0}", num2);
            Console.WriteLine("BigNumber #3: (#1 + #2) = {0}", num3);

            // Sum
            num3 = num1;
            num3 += num1;

            // Print
            Console.WriteLine("BigNumber #3: (#1 + #1) = {0}", num3);
        }
Ejemplo n.º 7
0
 // Always longer number is stored in num1
 private static int GetLength(ref BigNumbers num1, ref BigNumbers num2)
 {
     int len;
     if (num1.bigNumber.Length >= num2.bigNumber.Length)
     {
         // Length is the maximal length
         len = num1.bigNumber.Length;
     }
     else
     {
         // if num2 length > num1 length then swap num1 and num2
         len = num2.bigNumber.Length;
         BigNumbers tmp = num1;
         num1 = num2;
         num2 = tmp;
     }
     return len;
 }