Ejemplo n.º 1
0
 private static BigFraction Add(BigFraction left, BigFraction right, bool bReduce = false)
 {
     if (left.Denominator == right.Denominator)
     {
         return(new BigFraction(left.Numerator + right.Numerator, left.Denominator));
     }
     return(new BigFraction(left.Numerator * right.Denominator + right.Numerator * left.Denominator, right.Denominator * left.Denominator));
 }
Ejemplo n.º 2
0
        public bool Equals(BigFraction other)
        {
            BigFraction f1, f2;

            if (Denominator == other.Denominator)
            {
                return(Numerator == other.Numerator);
            }
            f1 = Reduce(this);
            f2 = Reduce(other);
            if (f1.Denominator == f2.Denominator)
            {
                return(f1.Numerator == f2.Numerator);
            }
            return(false);
        }
Ejemplo n.º 3
0
        //Fancy methods

        public static BigFraction SqrtConvergent(int iNum, int iConvergent)
        {
            if (iNum.IsSquare() || iConvergent == 0)
            {
                return(new BigFraction(iNum.Sqrt(), 1));
            }

            List <int> liContFrac = Numbers.SqrtContinuedFraction(iNum);
            int        iPeriod    = liContFrac.Count - 1;

            iConvergent--;
            BigFraction f = new BigFraction(liContFrac[(iConvergent % iPeriod) + 1], 1);

            for (int i = iConvergent - 1; i >= 0; i--)
            {
                f = liContFrac[(i % iPeriod) + 1] + (1 / f);
            }
            f = liContFrac[0] + (1 / f);
            return(f);
        }
Ejemplo n.º 4
0
        //Returns Tuple(x,y) for x^2 - Dy^2 = 1;
        public static Tuple <BigInteger, BigInteger> SolvePellEquation(int D)
        {
            BigInteger  x, y;
            List <int>  liSCF = Numbers.SqrtContinuedFraction(D);
            int         r     = Math.Max(liSCF.Count - 2, 1);
            BigFraction bf;

            if (r % 2 == 0)
            {
                bf = BigFraction.SqrtConvergent(D, 2 * r + 1);
            }
            else
            {
                bf = BigFraction.SqrtConvergent(D, r);
            }
            bf.Reduce();
            x = bf.Numerator;
            y = bf.Denominator;
            return(new Tuple <BigInteger, BigInteger>(x, y));
        }
Ejemplo n.º 5
0
 private static BigFraction Reduce(BigFraction f)
 {
     f.Reduce();
     return(f);
 }
Ejemplo n.º 6
0
 private static BigFraction Add(BigFraction f, BigInteger i)
 {
     return(new BigFraction(f.Numerator + (i * f.Denominator), f.Denominator));
 }