コード例 #1
0
 public static StringArithmetics Ceiling(CatRest a)
 {
     if (a.Rest >= 0)
     {
         return(new StringArithmetics((a.Cat + 1).Operand));
     }
     return(new StringArithmetics(a.Cat.Operand));
 }
コード例 #2
0
        public static StringArithmetics ToBinary(StringArithmetics a)
        {
            StringArithmetics bin = new StringArithmetics("");

            while (a != 0)
            {
                CatRest catRest = a / 2;
                bin.Operand += catRest.Rest.Operand;
                a            = catRest.Cat;
            }

            char[] charArray = bin.Operand.ToCharArray();
            Array.Reverse(charArray);
            bin.Operand = new String(charArray);

            return(bin);
        }
コード例 #3
0
        public static bool Parrat(StringArithmetics n)
        {
            if (n == 2 || n == 3)
            {
                return(true);
            }


            if ((n / 2).Rest == 0)
            {
                return(false);
            }


            for (StringArithmetics x = 2.ToStringArithmetics(); x <= n - 1; x = x + 1)
            {
                if ((((x ^ (n - 1)) - 1) / n).Rest != 0)
                {
                    continue;
                }


                List <StringArithmetics> factori = new List <StringArithmetics>();

                StringArithmetics q = n - 1;
                StringArithmetics i = 2.ToStringArithmetics();
                CatRest           r = new CatRest();

                while (q > 1)
                {
                    r = q / i;
                    if (r.Rest == 0)
                    {
                        factori.Add(i);
                        q = (q / i).Cat;
                    }
                    else
                    {
                        i = i + 1;
                    }
                }

                foreach (StringArithmetics a in factori)
                {
                    if (!Parrat(a))
                    {
                        return(false);
                    }
                }

                bool Ok = true;

                foreach (StringArithmetics a in factori)
                {
                    if ((((x ^ ((n - 1) / a).Cat) - 1) / n).Rest == 0)
                    {
                        Ok = false;
                    }
                }
                if (Ok)
                {
                    return(true);
                }
            }
            return(true);
        }
コード例 #4
0
        public static CatRest operator /(StringArithmetics a, Object n)
        {
            if (n is int)
            {
                StringArithmetics b = (Convert.ToInt32(n)).ToStringArithmetics();
                return(a / b);
            }
            if (n is StringArithmetics)
            {
                StringArithmetics b = new StringArithmetics((n as StringArithmetics).Operand);

                string pattern     = "^0*";
                string replacement = "";
                Regex  rgx         = new Regex(pattern);
                a.Operand = rgx.Replace(a.Operand, replacement);
                b.Operand = rgx.Replace(b.Operand, replacement);

                if (a.Operand.Length == 0)
                {
                    a.Operand = "0";
                }


                if (b.Operand.Length == 0)
                {
                    b.Operand = "0";
                }

                if (a == 0)
                {
                    return(new CatRest("0", "0"));
                }

                if (b == 1)
                {
                    return(new CatRest(a.Operand, "0"));
                }

                if (b < 0)
                {
                    CatRest catRest = a / -b;
                    return(new CatRest((-catRest.Cat).Operand, catRest.Rest.Operand));
                }

                if (a < 0)
                {
                    CatRest           catRest = -a / b;
                    StringArithmetics q       = -catRest.Cat - 1.ToStringArithmetics();
                    if (catRest.Rest > 0)
                    {
                        return(new CatRest((-catRest.Cat - 1.ToStringArithmetics()).Operand, (b - catRest.Rest).Operand));
                    }
                    return(new CatRest((-catRest.Cat).Operand, "0"));
                }

                int LengthA = a.Operand.Length;
                int LengthB = b.Operand.Length;

                if (LengthA < LengthB)
                {
                    return(new CatRest("0", a.Operand));
                }

                if (a <= b * 10)
                {
                    for (int q = 0; q <= 9; q++)
                    {
                        if (a < b * q + b)
                        {
                            CatRest catRest = new CatRest(q.ToString(), (a - b * q.ToStringArithmetics()).Operand);
                            return(catRest);
                        }
                    }
                }



                if (LengthA > LengthB)
                {
                    List <StringArithmetics> list = new List <StringArithmetics>();

                    //a.Operand = a.Operand.PadRight(a.Operand.Length + (b.Operand.Length -(a.Operand.Length % b.Operand.Length)), '0');
                    //LengthA = a.Operand.Length;

                    int startindex = 0;
                    int length     = 1;



                    while (startindex + length <= LengthA)
                    {
                        list.Add(new StringArithmetics(a.Operand.Substring(startindex, length)));
                        startindex += length;
                    }

                    CatRest catRest = new CatRest();

                    foreach (StringArithmetics val in list)
                    {
                        StringArithmetics pow = 10.ToStringArithmetics() ^ val.Operand.Length.ToStringArithmetics();
                        CatRest           aux = (val + (pow * catRest.Rest)) / b;
                        catRest.Cat  = catRest.Cat * pow + aux.Cat;
                        catRest.Rest = aux.Rest;
                    }

                    return(catRest);


                    //StringArithmetics q = Floor(a, b);
                    //return new CatRest(q.Operand, (a - q * b).Operand);

                    //List<StringArithmetics> y = new List<StringArithmetics>();
                    //StringArithmetics l = ((10.ToStringArithmetics() ^ B)/ b).Cat;
                    //StringArithmetics l2 = Floor(10.ToStringArithmetics() ^ A, a);

                    //if (2.ToStringArithmetics() * (10.ToStringArithmetics() ^ B) <= b * (1.ToStringArithmetics() + 2.ToStringArithmetics() * l))
                    //    y.Add((10.ToStringArithmetics() ^ (A - B)) * l);
                    //else
                    //    y.Add((10.ToStringArithmetics() ^ (A - B)) * (l + 1.ToStringArithmetics()));

                    //while (true)
                    //{
                    //    StringArithmetics f1 = Floor(y[y.Count - 1] ^ 2.ToStringArithmetics(), 10.ToStringArithmetics() ^ (A - B));
                    //    StringArithmetics f2 = Floor(b * f1, 10.ToStringArithmetics() ^ B);

                    //    y.Add(2.ToStringArithmetics() * y[y.Count - 1] - f2);

                    //    if (y[y.Count - 1] <= y[y.Count - 2])
                    //    {
                    //        break;
                    //    }
                    //}

                    //foreach (StringArithmetics val in y)
                    //{
                    //    if (Floor(a * b, 10.ToStringArithmetics() ^ (A - B)) == Floor(val * b, 10.ToStringArithmetics() ^ (A - B)))
                    //    {
                    //        StringArithmetics t = Floor(val * l, 10.ToStringArithmetics() ^ (A - B));
                    //        return new CatRest(t.Operand, (a - b * t).Operand);
                    //    }
                    //}
                }
                return(new CatRest("0", "0"));;
            }
            return(new CatRest("0", "0"));;
        }
コード例 #5
0
 public static StringArithmetics Floor(CatRest a)
 {
     return(new StringArithmetics(a.Cat.Operand));
 }