Exemplo n.º 1
0
        public Rational Root(BigInteger r)
        {
            /* test for overflow */
            if (r.CompareTo(MaxInt32) == 1)
            {
                throw new FormatException("Root " + r + " too large.");
            }
            if (r.CompareTo(MinInt32) == -1)
            {
                throw new FormatException("Root " + r + " too small.");
            }

            int rthroot = r.ToInt32();

            /* cannot pull root of a negative value with even-valued root */
            if (CompareTo(Zero) == -1 && (rthroot % 2) == 0)
            {
                throw new FormatException("Negative basis " + ToString() + " with odd root " + r);
            }

            /* extract a sign such that we calculate |n|^(1/r), still r carrying any sign
             */
            bool flipsign = (CompareTo(Zero) == -1 && (rthroot % 2) != 0);

            /* delegate the main work to ifactor#root()
             */
            var      num   = new IFactor(Numerator.Abs());
            var      deno  = new IFactor(Denominator);
            Rational resul = num.Root(rthroot).Divide(deno.Root(rthroot));

            return(flipsign ? resul.Negate() : resul);
        }
Exemplo n.º 2
0
 private void GrowTo(int n)
 {
     /* extend the internal list if needed. Size to be 2 for n<=1, 3 for n<=2 etc.
      */
     while (factors.Count <= n)
     {
         int lastn = factors.Count - 1;
         var nextn = new IFactor(lastn + 1);
         factors.Add(factors[lastn].Multiply(nextn));
     }
 }