コード例 #1
0
        public static VeryLong operator -(VeryLong vl1)
        {
            VeryLong result = new VeryLong(vl1);

            result.value[0] = -result.value[0];
            return(result);
        }
コード例 #2
0
 public static VeryLong Mod(VeryLong vl1)
 {
     if (vl1 < 0)
     {
         return(-vl1);
     }
     return(vl1);
 }
コード例 #3
0
        public static VeryLong operator -(VeryLong vl1, VeryLong vl2)
        {
            if (vl1 < 0 ^ vl2 < 0)
            {
                if (vl1 < 0)
                {
                    return(vl2 + -vl1);
                }
                else
                {
                    return(vl1 + -vl2);
                }
            }
            else if (vl1 < 0 && vl2 < 0)
            {
                return(vl2 - -vl1);
            }
            else if (vl1 < vl2)
            {
                return(-(vl2 - vl1));
            }
            VeryLong result = new VeryLong(vl1);

            for (int i = vl2.value.Count - 1; i >= 0; i--)
            {
                if (result.value[result.value.Count - vl2.value.Count + i] - vl2.value[i] >= 0)
                {
                    result.value[result.value.Count - vl2.value.Count + i] -= vl2.value[i];
                }
                else
                {
                    var temp  = ((ulong)cbase - (ulong)vl2.value[i]);
                    var temp1 = (int)temp;

                    result.value[result.value.Count - vl2.value.Count + i] += (int)((ulong)cbase - (ulong)vl2.value[i]);
                    result.value[result.value.Count - vl2.value.Count + i - 1]--;
                }
            }
            for (int i = 0; i < result.value.Count - 1; i++)
            {
                if (result.value[i] == 0)
                {
                    result.value.RemoveAt(0);
                    i--;
                }
                else
                {
                    break;
                }
            }
            return(result);
        }
コード例 #4
0
 public override bool Equals(object obj)
 {
     //Check for null and compare run-time types.
     if ((obj == null) || !GetType().Equals(obj.GetType()))
     {
         return(false);
     }
     else
     {
         VeryLong vl2 = (VeryLong)obj;
         return(vl2.ToString() == ToString());
     }
 }
コード例 #5
0
 public static VeryLong Pow(VeryLong number, VeryLong pow)
 {
     if (pow == 0)
     {
         return(new VeryLong(1));
     }
     else if (pow % 2 == 0)
     {
         return(Pow(number, pow / 2) * Pow(number, pow / 2));
     }
     else
     {
         return(number * Pow(number, pow / 2) * Pow(number, pow / 2));
     }
 }
コード例 #6
0
 static VeryLong Multiplication(VeryLong vl1, VeryLong vl2)
 {
     if (vl1.value.Count == 1 && vl2.value.Count == 1)
     {
         return(new VeryLong((ulong)vl1.value[0] * (ulong)vl2.value[0]));
     }
     else
     {
         VeryLong result = new VeryLong();
         if (vl1.value.Count <= vl2.value.Count)
         {
             for (int i = vl1.value.Count - 1; i >= 0; i--)
             {
                 for (int j = vl2.value.Count - 1; j >= 0; j--)
                 {
                     VeryLong temp = new VeryLong((ulong)vl1.value[i] * (ulong)vl2.value[j]);
                     temp.value.InsertRange(temp.value.Count, new int[vl2.value.Count + vl1.value.Count - j - i - 2]);
                     result += temp;
                 }
             }
         }
         else
         {
             for (int i = vl2.value.Count - 1; i >= 0; i--)
             {
                 for (int j = vl1.value.Count - 1; j >= 0; j--)
                 {
                     VeryLong temp = new VeryLong((ulong)vl2.value[i] * (ulong)vl1.value[j]);
                     temp.value.InsertRange(temp.value.Count, new int[vl1.value.Count + vl2.value.Count - j - i - 2]);
                     result += temp;
                 }
             }
         }
         if (result.value[0] == 0)
         {
             result.value.RemoveAt(0);
         }
         return(result);
     }
 }
コード例 #7
0
        public static VeryLong operator +(VeryLong vl1, VeryLong vl2)
        {
            if (vl1 < 0 ^ vl2 < 0)
            {
                if (vl1 < 0)
                {
                    return(vl2 - -vl1);
                }
                else
                {
                    return(vl1 - -vl2);
                }
            }
            else if (vl1 < 0 && vl2 < 0)
            {
                return(-(-vl1 + -vl2));
            }
            VeryLong result = new VeryLong();

            if (vl1.value.Count <= vl2.value.Count)
            {
                result = new VeryLong(vl2);
                for (int i = vl1.value.Count - 1; i >= 0; i--)
                {
                    if (result.value[result.value.Count - vl1.value.Count + i] + vl1.value[i] < Math.Pow(10, lbase))
                    {
                        result.value[result.value.Count - vl1.value.Count + i] += vl1.value[i];
                    }
                    else
                    {
                        result.value[result.value.Count - vl1.value.Count + i] += vl1.value[i] - cbase;
                        if (result.value.Count - vl1.value.Count + i != 0)
                        {
                            result.value[result.value.Count - vl1.value.Count + i - 1]++;
                        }

                        else
                        {
                            result.value.Insert(0, 1);
                        }
                    }
                }
            }
            else
            {
                result = new VeryLong(vl1);
                for (int i = vl2.value.Count - 1; i >= 0; i--)
                {
                    if (result.value[result.value.Count - vl2.value.Count + i] + vl2.value[i] < Math.Pow(10, lbase))
                    {
                        result.value[result.value.Count - vl2.value.Count + i] += vl2.value[i];
                    }
                    else
                    {
                        result.value[result.value.Count - vl2.value.Count + i] += vl2.value[i] - cbase;
                        if (result.value.Count - vl2.value.Count + i != 0)
                        {
                            result.value[result.value.Count - vl2.value.Count + i - 1]++;
                        }

                        else
                        {
                            result.value.Insert(0, 1);
                        }
                    }
                }
            }
            return(result);
        }
コード例 #8
0
 public VeryLong(VeryLong value) : this(new List <int>(value.value))
 {
 }
コード例 #9
0
        public static void Main(string[] args)
        {
            string input = "4511263156549903003502425944020621017624018923015950620210037836734253630^200".Replace(" ", "").Replace("(", "").Replace(")", "");

            #region ifs
            if (input.Contains("^"))
            {
                string[] numbers = input.Split('^');
                if (numbers.Length <= 1)
                {
                    Console.WriteLine("Слишком мало чисел");
                    return;
                }
                VeryLong number = new VeryLong(numbers[0]);
                for (int i = 1; i < numbers.Length; i++)
                {
                    number = VeryLong.Pow(number, new VeryLong(numbers[i]));
                }
                Console.WriteLine(number.ToString());
            }
            else if (input.Contains("/") || input.Contains(":"))
            {
                string[] numbers;
                if (input.Contains("/"))
                {
                    numbers = input.Split('/');
                }
                else
                {
                    numbers = input.Split(':');
                }
                if (numbers.Length <= 1)
                {
                    Console.WriteLine("Слишком мало чисел");
                    return;
                }
                VeryLong number = new VeryLong(numbers[0]);
                for (int i = 1; i < numbers.Length; i++)
                {
                    number /= new VeryLong(numbers[i]);
                }
                Console.WriteLine(number.ToString());
            }
            else if (input.Contains("*"))
            {
                string[] numbers = input.Split('*');
                if (numbers.Length <= 1)
                {
                    Console.WriteLine("Слишком мало чисел");
                    return;
                }
                VeryLong number = new VeryLong(numbers[0]);
                for (int i = 1; i < numbers.Length; i++)
                {
                    number *= new VeryLong(numbers[i]);
                }
                Console.WriteLine(number.ToString());
            }
            #endregion
            else if (input.Contains("+"))
            {
                string[] numbers = input.Split('+');
                if (numbers.Length <= 1)
                {
                    Console.WriteLine("Слишком мало чисел");
                    return;
                }
                VeryLong number = new VeryLong(numbers[0]);
                for (int i = 1; i < numbers.Length; i++)
                {
                    number += new VeryLong(numbers[i]);
                }
                Console.WriteLine(number.ToString());
            }
            else if (input.Contains("-"))
            {
                List <string> numbers = new List <string>(input.Replace("--", "ё").Split("-".ToCharArray()));
                for (int i = 0; i < numbers.Count; i++)
                {
                    if (numbers[i].Contains("ё"))
                    {
                        numbers.InsertRange(i, numbers[i].Split('ё'));
                        numbers.RemoveAt(i + 2);
                        i++;
                        numbers[i] = "-" + numbers[i];
                    }
                }
                if (numbers.Count <= 1)
                {
                    Console.WriteLine("Слишком мало чисел");
                    return;
                }
                for (int i = 0; i < numbers.Count - 1; i++)
                {
                    if (numbers[i] == "")
                    {
                        numbers[i + 1].Insert(0, "-");
                        numbers.RemoveAt(i);
                        i--;
                    }
                }
                VeryLong number = new VeryLong(numbers[0]);
                for (int i = 1; i < numbers.Count; i++)
                {
                    number -= new VeryLong(numbers[i]);
                }
                Console.WriteLine(number.ToString());
            }
        }
コード例 #10
0
        public static VeryLong operator /(VeryLong vl1, VeryLong vl2)
        {
            if (vl1 == 0)
            {
                return(new VeryLong(0));
            }
            else if (vl2 == 0)
            {
                throw new DivideByZeroException();
            }
            else if (vl1 < 0 ^ vl2 < 0)
            {
                if (vl1 < 0)
                {
                    return(-(-vl1 / vl2));
                }
                else
                {
                    return(-(vl1 / -vl2));
                }
            }
            else if (vl1 < 0 && vl2 < 0)
            {
                return(-vl1 / -vl2);
            }
            else if (vl2 == 2)
            {
                VeryLong result = vl1 * 5;
                int      temp   = 0;

                for (int i = 0; i < result.value.Count; i++)
                {
                    if (i == 0 && result.value[i] / 10 == 0)
                    {
                        temp = result.value[i] % 10;
                        if (result.value.Count != 1)
                        {
                            result.value.RemoveAt(0);
                            if (i < result.value.Count)
                            {
                                int temp1 = result.value[i] % 10;
                                result.value[i] /= 10;
                                result.value[i] += temp * (cbase / 10);
                                temp             = temp1;
                            }
                        }
                        else
                        {
                            return(new VeryLong());
                        }
                    }
                    else
                    {
                        int temp1 = result.value[i] % 10;
                        result.value[i] /= 10;
                        result.value[i] += temp * (cbase / 10);
                        temp             = temp1;
                    }
                }
                return(result);
            }
            VeryLong up = new VeryLong(vl1);

            up.value[0] = 1;
            VeryLong down = new VeryLong();

            while (up - down != 1)
            {
                if (vl2 * ((up + down) / 2) < vl1)
                {
                    down += (up - down) / 2;
                }
                else if (vl2 * ((up + down) / 2) >= vl1)
                {
                    up -= (up - down) / 2;
                }
            }
            if (vl2 * up == vl1)
            {
                return(up);
            }
            else
            {
                return(down);
            }
        }