Esempio n. 1
0
        public static HugeInteger operator *(HugeInteger initial, HugeInteger multiplied)
        {
            int         sign      = initial.sign * multiplied.sign;
            HugeInteger newNumber = new HugeInteger("0");

            for (int i = multiplied.maxLength - 1; i >= multiplied.maxLength - multiplied.Length; i--)
            {
                int        addition      = 0;
                List <int> initialNumber = new List <int>();

                for (int j = initial.maxLength - 1; j >= initial.maxLength - initial.Length; j--)
                {
                    int m = multiplied.number[i] * initial.number[j] + addition;
                    initialNumber.Insert(0, m % 10);
                    addition = m / 10;
                }

                if (addition != 0)
                {
                    initialNumber.Insert(0, addition);
                }
                newNumber += new HugeInteger(String.Join("", initialNumber.ToArray()).PadRight(initialNumber.Count + (multiplied.maxLength - i - 1), '0'));
            }

            return(new HugeInteger(newNumber.number, sign));
        }
Esempio n. 2
0
 protected static int CompareUnsigned(HugeInteger a, HugeInteger b)
 {
     if (a.Length == b.Length)
     {
         return(String.Compare(a.ToString().TrimStart('-'), b.ToString().TrimStart('-')));
     }
     return((a.Length > b.Length) ? 1 : -1);
 }
Esempio n. 3
0
        public override bool Equals(object value)
        {
            HugeInteger number = value as HugeInteger;

            return(!Object.ReferenceEquals(null, number) &&
                   String.Equals(this.sign, number.sign) &&
                   String.Equals(this.number, number.number));
        }
Esempio n. 4
0
        private static (HugeInteger, string) DivideBySub(HugeInteger initial, HugeInteger divided)
        {
            int q = 0;

            while (initial >= divided)
            {
                initial -= divided;
                q++;
            }

            return(initial, q.ToString());
        }
Esempio n. 5
0
        public static bool operator >(HugeInteger a, HugeInteger b)
        {
            if (a.sign < b.sign)
            {
                return(false);
            }
            else if (a.sign > b.sign)
            {
                return(true);
            }
            int comp = HugeInteger.CompareUnsigned(a, b);

            return(comp == a.sign);
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            HugeInteger h1 = new HugeInteger("64623742387434762374682374623874");
            HugeInteger h2 = new HugeInteger("65");

            Console.WriteLine(new HugeInteger("10") * new HugeInteger("0"));
            // Console.WriteLine(new HugeInteger("10") / new HugeInteger("0"));
            Console.WriteLine(h1 * h2);
            Console.WriteLine(h1 / h2);
            Console.WriteLine(h1 % h2);
            Console.WriteLine(h1 + h2);
            Console.WriteLine(h1 - h2);
            Console.WriteLine(h1 > h2);
            Console.WriteLine(h1 < h2);
            Console.WriteLine(h1 == h2);
            Console.WriteLine(h1 >= h2);
            Console.WriteLine(h1 <= h2);
            Console.WriteLine(h1 != h2);
        }
Esempio n. 7
0
        public static int CompareUnsigned(HugeInteger a, HugeInteger b)
        {
            if (a.Length == b.Length)
            {
                for (int i = a.Length - 1; i >= 0; i--)
                {
                    if (a.number[i] > b.number[i])
                    {
                        return(1);
                    }
                    else if (a.number[i] < b.number[i])
                    {
                        return(-1);
                    }
                }

                return(0);
            }
            return((a.Length > b.Length) ? 1 : -1);
        }
Esempio n. 8
0
        private static (HugeInteger, string) DivideBySub(HugeInteger initial, HugeInteger divided)
        {
            int   q = 0;
            ulong a, b = 0;

            if (ulong.TryParse(initial.ToString(), out a) && ulong.TryParse(divided.ToString(), out b))
            {
                return(new HugeInteger((a % b).ToString()), (a / b).ToString());
            }

            HugeInteger d = new HugeInteger(divided.number, -divided.sign, true);

            while (initial >= divided)
            {
                initial += d;
                q++;
            }

            return(initial, q.ToString());
        }
Esempio n. 9
0
        public static HugeInteger operator +(HugeInteger initial, HugeInteger added)
        {
            int         compare = HugeInteger.CompareUnsigned(initial, added);
            HugeInteger max, min;

            (max, min) = (compare == -1) ? (added, initial) : (initial, added);

            int[] initialNumber = max.number;
            bool  swap          = false;

            int method = (max.sign == min.sign) ? 1 : -1;

            if (method == -1 && max.sign == -1)
            {
                max.sign = -max.sign; min.sign = -min.sign; swap = true;
            }

            try
            {
                int i = initial.maxLength - 1;
                while (i >= max.maxLength - max.Length)
                {
                    int sum = (method == 1) ? initialNumber[i] + min.number[i] : max.sign * initialNumber[i] + min.sign * min.number[i];
                    initialNumber[i] = (sum < 0) ? (10 + sum) : (sum % 10);
                    if (sum >= 10 || sum < 0)
                    {
                        initialNumber[i - 1] += (sum < 0) ? -1 : ((sum >= 10) ? 1 : 0);
                    }
                    i--;
                }

                if (swap)
                {
                    max.sign = -max.sign; min.sign = -min.sign;
                }
            }
            catch (IndexOutOfRangeException) { throw new OverflowException(); }

            return(new HugeInteger(initialNumber, max.sign));
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            HugeInteger h1 = new HugeInteger("476914796134666236301825281227691241134474621043394087818275768911914506422315347259376870106819225299456860317082527213310907303509855855476484541285855594730868217252660920224263417830804958939696689300372903565001336612224447115795154881740747284159955332834113300650377805012799172278327996181593333539874466937854764701081103976764624178897829183568567223329823363620423261064860270596464048583606423302627736585021398818704742409188233725898265845372667180276802758400517881366965533431542010284694908996611352551966426162250103239479801052870046817353291065191517021496367245735557993248559980824549105693001287394182560868625063129625314319594035967108012310904399380358578135587703293964659488133939598212161766296631947941051313042115901703733907616668236530881395674171021105443807246734456429850511593239558191991979714028464877965247763746398058177400283219923373644709990149049540355388825823522723461860787105035112182946875951324106716513403610131710454274324208357367002187171191813062353775572783773574194985076388466511327958062791587782812274410405367589582994382441462799131007449083122978389911787896624154022836309667267895810086884181333383037764082415091316727796227040150331564259194867884241604924743007361420333864290854989740837860820120218666280793985266951532732543532398345618850651784703961043152465055859962379262703019296506825431167690474942108388586064281613162899698339393506673053232957806722852069458580984852855426240810700756140247125855018950027452957574599105490537145912865427822721548375421110547487675147917798738805120182350888517668822419700555239212183472154487696917702644801679294660592174378517650557777089930812304888213566763098038879437916454749679686210204244980879490236808094780505684674511480854859336481130154554233428293613540523878496757243128240671755022284778797577593305982610486124550634003268426964785787176756486845512773105329170526179556650207833913647217613952773452332761012244060707330778756784525599801117641460943944544991996096787479805160472237501231506724005236661275226188819243574912186053983719073395618488336376159612388203576897118515016785839072328364892616213427171209521672198378814342097750251067359853595686113324539723333169135621021201180831662059577342576903763974879616560250543852516141017914687566041949988755703379223894047313109929123003357336881258879300712965115620182070116059434005187593802790692995801531233000514376257983844917849862986664751482029976907160149347368585749902378169142579820415612571355070251505868346930511822726024326089896050648869771969946090607438371589766498652303842423443238371585088596243863070859776880725000680027290934131046710790774007714915284336404864294653645013710014825974868312280298049472847596641417942743598469592760477199882827637224038451026486476430536860013050471113245971932795606929394811670967799939352777414138640995906288796543061449632169926672984603315362541077542647683218385147608827988475761583173830546168352535993580169948634511138376718076325301721274852810747152267969292270838518998214249612497061345922841510623051205624176414901412068146851670197218189932288793578850492269663593467704122897236782916298069389471947706253457974564279064362686316933950190714998540060390956331369674724246892876513488287302647159236870936020369536803180419732454008513517908646223519761557934579253803865719027875664927709201021819225600621600805053126321457644661684642998120615980854385593074336224572659907556676065524211507395065971352515578560082562561521414188906528426766920999038594223247686543010866244997400118757157347302212353042432973689270482667489215606292510950557055144703884422935158069139539570836670845731111565019965392750455589740323327639622468523996775239812293745308246633484926777732675138662431483952692924677853734240312706058651146698377092977927600144210829414370349494959811201209328195568842023135120945082030513848880568547270549859141796344578641890158384834133377563319167971478323255351289480692892995737591405594567137576747752886332174409382875292345676449590484272408440994290319398576919345715003");
            HugeInteger h2 = new HugeInteger("2112312");
            // Console.WriteLine(new HugeInteger("10") * new HugeInteger("0"));
            // Console.WriteLine(new HugeInteger("10") / new HugeInteger("0"));
            // Console.WriteLine(h1 * h2);
            Stopwatch sw = new Stopwatch();

            sw.Start();
            h1 /= h2;
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
            // Console.WriteLine(h1);
            // Console.WriteLine(h1 % h2);
            // Console.WriteLine(h1 + h2);
            // Console.WriteLine(h1 - h2);
            // Console.WriteLine(h1 > h2);
            // Console.WriteLine(h1 < h2);
            // Console.WriteLine(h1 == h2);
            // Console.WriteLine(h1 >= h2);
            // Console.WriteLine(h1 <= h2);
            // Console.WriteLine(h1 != h2);
        }
Esempio n. 11
0
 public static bool operator >=(HugeInteger a, HugeInteger b)
 {
     return(a > b || HugeInteger.CompareUnsigned(a, b) == 0 && a.sign == b.sign);
 }
Esempio n. 12
0
 public static HugeInteger operator %(HugeInteger initial, HugeInteger divided) => new HugeInteger(HugeInteger.Divide(initial, divided).Item2);
Esempio n. 13
0
        private static (string, string) Divide(HugeInteger initial, HugeInteger divided)
        {
            int sign = initial.sign * divided.sign; initial.sign = 1; divided.sign = 1;

            if (initial < divided)
            {
                return("0", initial.ToString());
            }
            if (divided.isZero())
            {
                throw new DivideByZeroException();
            }
            if (divided.ToString() == "1")
            {
                return(((sign == -1) ? "-" : "") + initial.ToString(), "0");
            }

            int zeros = Math.Min(divided.Length - divided.ToString().TrimEnd('0').Length, initial.Length - initial.ToString().TrimEnd('0').Length);

            divided = new HugeInteger(divided.ToString().Substring(0, divided.Length - zeros));
            initial = new HugeInteger(initial.ToString().Substring(0, initial.Length - zeros));

            HugeInteger newNumber;
            string      dividedNumberStr = divided.ToString();

            string q, tmp, sliceI, tail; q = tmp = tail = sliceI = "";
            bool   first = true;
            int    i, count;
            int    lastLength = divided.Length;


            while (initial >= divided)
            {
                i      = initial.maxLength - initial.Length;
                sliceI = String.Join("", initial.number.Skip(i).Take(lastLength + ((first) ? 0 : 1)));
                count  = (first) ? 1 : 2;

                while (HugeInteger.CompareUnsigned(sliceI, dividedNumberStr) < 0)
                {
                    if (!first)
                    {
                        q += "0";
                    }
                    sliceI = String.Join("", initial.number.Skip(i).Take(lastLength + count));
                    count++;
                }

                first = false;

                (newNumber, tmp) = HugeInteger.DivideBySub(new HugeInteger(sliceI), new HugeInteger(dividedNumberStr));
                q         += tmp;
                lastLength = newNumber.Length;
                tail       = String.Join("", initial.number.Skip(initial.maxLength - initial.Length + sliceI.Length).Take(initial.maxLength - (initial.maxLength - initial.Length + sliceI.Length)));
                initial    = new HugeInteger(newNumber.ToString() + tail);
            }

            if (tail != "")
            {
                q = q.PadRight(q.Length + tail.Length, '0');
            }

            return(((sign == -1) ? "-" : "") + q, initial.ToString());
        }
Esempio n. 14
0
        private static (string, string) Divide(HugeInteger initial, HugeInteger divided)
        {
            int sign = initial.sign * divided.sign; initial.sign = 1; divided.sign = 1;

            if (initial < divided)
            {
                return("0", initial.ToString());
            }
            if (divided.isZero())
            {
                throw new DivideByZeroException();
            }
            if (divided.ToString() == "1")
            {
                return(((sign == -1) ? "-" : "") + initial.ToString(), "0");
            }

            int zeros = Math.Min(divided.Length - String.Join("", divided.number).TrimStart('0').Length, initial.Length - String.Join("", initial.number).TrimStart('0').Length);

            string dividedNumberStr = String.Join("", divided.number.Skip(zeros).Take(divided.Length - zeros).Reverse()).TrimStart('0');
            string initialNumberStr = String.Join("", initial.number.Skip(zeros).Take(initial.Length - zeros).Reverse()).TrimStart('0');

            HugeInteger newNumber;

            string q, tmp, tail; q = tmp = tail = "";
            bool   first = true;
            bool   zero  = true;
            string sliceI;
            int    count;
            int    lastLength = divided.Length;

            while (HugeInteger.CompareUnsigned(initialNumberStr, dividedNumberStr) >= 0)
            {
                count = (zero) ? 0 : 1;
                int sliceLength = lastLength + count;
                sliceI = String.Join("", initialNumberStr.Take(sliceLength));
                count++;


                while (HugeInteger.CompareUnsigned(sliceI, dividedNumberStr) < 0)
                {
                    if (!first)
                    {
                        q += "0";
                    }
                    sliceI += initialNumberStr[sliceI.Length].ToString();
                    count++;
                }

                first = false;

                (newNumber, tmp) = HugeInteger.DivideBySub(new HugeInteger(sliceI), divided);

                zero = newNumber.isZero() ? true : false;

                q         += tmp;
                lastLength = newNumber.Length;
                tail       = String.Join("", initialNumberStr.Skip(sliceI.Length).Take(initial.Length - sliceI.Length));

                initialNumberStr = ((!newNumber.isZero()) ? newNumber.ToString() : "") + tail;
            }

            if (tail != "")
            {
                q = q.PadRight(q.Length + tail.Length, '0');
            }


            return(((sign == -1) ? "-" : "") + q, initial.ToString());
        }