public static Fraction operator *(Fraction f1, Fraction f2) { int p = f1.p * f2.q - f2.p * f1.q; int q = f1.q * f2.q; int nod = EuclidFunctions.Euclid(p, q); p /= nod; q /= nod; return(new Fraction(p, q)); }
public Fraction(int p, int q) { this.p = p; this.q = q; if (p > 0 && q > 0 || p < 0 && q > 0) { } else if (p > 0 && q < 0 || q < 0 && p < 0) { this.p = -p; this.q = -q; } else if (p == 0) { } int nod = EuclidFunctions.Euclid(p, q); p = p / nod; q = q / nod; }
public static int ChineseRemainderTheorem(int[] res, int[] mod) { try { int compositionOfModuls = mod[0]; int inverseElement; int temp; for (int i = 1; i < mod.Length; i++) { try { temp = 1 / (EuclidFunctions.Euclid(compositionOfModuls, mod[i]) - 1); throw new Exception("may have no answer"); } catch { compositionOfModuls *= mod[i]; } //если числа не взаимно простые, решений может не быть. } //реализация китайской теоремы об остатках int result = 0; for (int i = 0; i < mod.Length; i++) { EuclidFunctions.ExtendedEuclid(compositionOfModuls / mod[i], mod[i], out inverseElement, out temp); result += res[i] * (compositionOfModuls / mod[i]) * inverseElement; } if (result < 0) { result %= compositionOfModuls; result += compositionOfModuls; } return(result); } catch { throw new Exception("error input"); } }
public static int GetMulInverse(int element, int mod) { EuclidFunctions.ExtendedEuclid(element, mod, out int inverseElement, out _); return(inverseElement); }