Ejemplo n.º 1
0
        public override MMC.Numbers.CNumber sqr()
        {
            int cnt = _Values.Count;

            // calc the different interims (could be done in parallel)
            CNumber_Integer[] Results = new CNumber_Integer[cnt];
            for (int i = 0; i < cnt; ++i)
            {
                CNumber_Integer tmp = new CNumber_Integer(this);
                tmp.Multiply(_Values[i]);
                tmp._Values.InsertRange(0, new byte[i]);
                Results[i] = tmp;
            }

            // now add them all up
            CNumber_Integer res = new CNumber_Integer(Results[0]);

            for (int i = 1; i < cnt; i++)
            {
                res.Addition(Results[i]);
            }

            res.Trim();
            return(res);
        }
Ejemplo n.º 2
0
        //------------------------------------------------------------
        // divide by another number and return the remainder
        protected virtual CNumber_Integer Divide(CNumber_Integer other)
        {
            CNumber_Integer Save = new CNumber_Integer(this);       // save the old value

            setZero();                                              // set the initial result to 0

            int o_len = other._Values.Count;
            int t_len = _Values.Count;
            int pos   = t_len - o_len;

            if (pos < 0)
            {
                return(Save);                                       // fewer digits means the result is zero and the remainder are just we
            }
            CNumber_Integer Rem = new CNumber_Integer();            // set the remainder to 0
            int             len = o_len - 1;

            while (pos >= 0)
            {
                Rem.InsertDigit(Save._Values[pos]);                 // take the next digit

                byte q = Rem._Values[len];
                q /= other._Values[len];                            // and make an estimate for the quotient

                CNumber_Integer tmp = new CNumber_Integer(other);   // and calculate the product for this estimated value
                tmp._Sign = false;
                tmp.Multiply(q);
                Rem.Subtraction(tmp);                               // see how good we estimated
                if (Rem._Sign)                                      // if we are too low just correct by one
                {
                    --q;
                    Rem.add(other);
                }

                InsertDigit(q);                                     // and write down the quotients digit
                --pos;                                              // walk one further in our (this) digits
            }

            _Sign = (_Sign != other._Sign);
            Trim();
            return(Rem);
        }
Ejemplo n.º 3
0
        // written multiplication
        public override MMC.Numbers.CNumber mul(MMC.Numbers.CNumber other)
        {
            if (other.MyType != MyType)
            {
                throw new NotImplementedException();
            }

            CNumber_Integer o   = (CNumber_Integer)other;
            int             cnt = o._Values.Count;

            // calc the different interims (could be done in parallel)
            CNumber_Integer[] Results = new CNumber_Integer[cnt];
            for (int i = 0; i < cnt; i++)
            {
                CNumber_Integer tmp = new CNumber_Integer(this);
                tmp.Multiply(o._Values[i]);
                tmp._Values.InsertRange(0, new byte[i]);
                Results[i] = tmp;
            }

            // now add them all up
            CNumber_Integer res = new CNumber_Integer(Results[0]);

            for (int i = 1; i < cnt; i++)
            {
                res.Addition(Results[i]);
            }

            //CNumber_Integer mul = (CNumber_Integer)other;
            //for (int i = mul._Values.Count - 1; i >= 0; i--)
            //{
            //    CNumber_Integer tmp = (CNumber_Integer) Clone();
            //    tmp.Multiply(mul._Values[i]);
            //    res.Insert(0);
            //    res.Addition(tmp);
            //}

            res._Sign = (_Sign != o._Sign);
            res.Trim();
            return(res);
        }