public ContinuedFraction(int n) { int root = (int)Math.Sqrt(n); if(root * root == n) { throw new ArgumentException("N can't be a perfect square"); } continuedFraction = new List<long>(); HashSet<String> store = new HashSet<String>(); RadicalFraction current = new RadicalFraction(n, 0, 1, 1, 0); while(!store.Contains(current.GetKey())) { store.Add(current.GetKey()); long integerPart = current.GetIntegerPart(); continuedFraction.Add(integerPart); current = current.MinusFraction(new RadicalFraction(n, integerPart, 0, 1, 0)); current = current.GetReciprocal(); current.Simplify(); } h = new BigInteger[] { 0, 1 }; k = new BigInteger[] { 1, 0 }; index = 0; }
public RadicalFraction MinusFraction(RadicalFraction other) { if (other.radical != this.radical) { throw new ArgumentException("Radicals differ"); } if (other.denominator[0] != this.denominator[0] || other.denominator[1] != this.denominator[1]) { other = other.Multiply(new RadicalFraction(radical, denominator[0], denominator[1], denominator[0], denominator[1])); } long[] newNumerator = new long[2]; newNumerator[0] = this.numerator[0] - other.numerator[0]; newNumerator[1] = this.numerator[1] - other.numerator[1]; long[] newDenominator = new long[2]; newDenominator[0] = this.denominator[0]; newDenominator[1] = this.denominator[1]; return new RadicalFraction(radical, newNumerator, newDenominator); }
public RadicalFraction Multiply(RadicalFraction other) { if (other.radical != this.radical) { throw new ArgumentException("Radicals differ"); } long[] newNumerator = new long[2]; newNumerator[0] = this.numerator[0] * other.numerator[0] + this.numerator[1] * other.numerator[1] * radical; newNumerator[1] = this.numerator[0] * other.numerator[1] + this.numerator[1] * other.numerator[0]; long[] newDenominator = new long[2]; newDenominator[0] = this.denominator[0] * other.denominator[0] + this.denominator[1] * other.denominator[1] * radical; newDenominator[1] = this.denominator[0] * other.denominator[1] + this.denominator[1] * other.denominator[0]; RadicalFraction result = new RadicalFraction(radical, newNumerator, newDenominator); return result; }
public RadicalFraction Divide(RadicalFraction other) { if (other.radical != this.radical) { throw new ArgumentException("Radicals differ"); } return this.Multiply(other.GetReciprocal()); }