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)); }
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); }
//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); }
//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)); }
private static BigFraction Reduce(BigFraction f) { f.Reduce(); return(f); }
private static BigFraction Add(BigFraction f, BigInteger i) { return(new BigFraction(f.Numerator + (i * f.Denominator), f.Denominator)); }