internal RawNum ExtendedGcd(RawNum left, RawNum right, out OmgNum x, out OmgNum y) { if (left.IsZero()) { x = 0.ToOmgNum(); y = 1.ToOmgNum(); RawNum gcd = OmgPool.GetRawZero(); gcd.CopyFrom(right); return(gcd); } var divModRes = OmgOp.DivMod(right.OmgWrapper(), left.OmgWrapper()); RawNum gcdRes = ExtendedGcd(divModRes.mod.Raw, left, out OmgNum x2, out OmgNum y2); divModRes.mod.Release(); OmgNum multRes = OmgOp.Multiply(divModRes.div, x2); divModRes.div.Release(); x = OmgOp.Subtract(y2, multRes); y2.Release(); multRes.Release(); y = x2; return(gcdRes); }
static void Main(string[] args) { var a = 234234.ToOmgNum(); var b = "85430534954938530495935803495482304983".ToOmgNum(); var c = "100000000000000000000000000000000".ToOmgNum(); var d = "-100000000000000000".ToOmgNum(); var a1 = OmgOp.Add(a, c); var a2 = OmgOp.Subtract(a, c); var a3 = OmgOp.Multiply(a, b); var a4 = OmgOp.DivMod(b, c); var a6 = OmgOp.Pow(b, c, a); var a7 = OmgOp.Sqrt(b); var sys1 = new OmgEqSys(); sys1.AddEq(1287.ToOmgNum(), 447.ToOmgNum(), 516.ToOmgNum()); bool success1 = sys1.TrySolve(out List <(OmgNum root, OmgNum mod)> solution1); var sys2 = new OmgEqSys(); bool success2 = sys2.TrySolve(out var solution2); }
private void _FactorPowerOfTwo(OmgNum num) { m_powerOfTwo?.Release(); m_initialTested?.Release(); var n = OmgOp.Subtract(num, OmgNum.GetConst(1)); m_powerOfTwo = 0.ToOmgNum(); (OmgNum div, OmgNum mod)divMod = OmgOp.DivMod(n, OmgNum.GetConst(2)); while (divMod.mod.IsZero()) { n.Release(); divMod.mod.Release(); n = divMod.div; m_powerOfTwo.Inc(); divMod = OmgOp.DivMod(n, OmgNum.GetConst(2)); } divMod.div.Release(); divMod.mod.Release(); m_initialTested = n; }
private OmgNum _FindPow(ref OmgNum num, OmgNum factor) { OmgNum pow = 0.ToOmgNum(); while (true) { var divMod = OmgOp.DivMod(num, factor); if (!divMod.mod.IsZero()) { break; } pow.Inc(); divMod.mod.Release(); num.Release(); num = divMod.div; } if (pow.IsZero()) { throw new Exception(); } return(pow); }
private OmgNum _FermaFactorization(Dictionary <OmgNum, OmgNum> result, OmgNum num) { OmgNum factor = null; if (OmgOp.DivMod(num, 2.ToOmgNum()).mod.IsZero()) { factor = 2.ToOmgNum(); } if (factor == null) { OmgNum a = OmgOp.Sqrt(num); OmgNum aSq = OmgOp.Multiply(a, a); bool isSquare = OmgOp.Equal(num, aSq); if (isSquare) { factor = a; } a.Inc(); aSq = OmgOp.Multiply(a, a); var s = OmgOp.Subtract(aSq, num); while (factor == null) { var testSqrt = OmgOp.Sqrt(s); if (OmgOp.Equal(s, OmgOp.Multiply(testSqrt, testSqrt))) { factor = (OmgOp.Equal(testSqrt, a)) ? OmgOp.Add(a, a) : OmgOp.Subtract(a, testSqrt); break; } a.Inc(); aSq = OmgOp.Multiply(a, a); s = OmgOp.Subtract(aSq, num); } } if (factor == null || factor.IsOne()) { return(num); } OmgNum pow = _FindPow(ref num, factor); if (result.ContainsKey(factor)) { result[factor] = OmgOp.Add(result[factor], pow); } else { result[factor] = pow; } return(num); }
private void _MakeGeronIteration() { OmgNum rootAprox = m_rootAprox.OmgWrapper(); var res = OmgOp.DivMod(m_targetSquareNum, rootAprox); res.mod.Release(); OmgNum sum = OmgOp.Add(rootAprox, res.div); res.div.Release(); sum.Raw.DivByTwo(); OmgPool.ReleaseNumber(m_buffer); m_buffer = m_rootAprox; m_rootAprox = sum.Raw; }
internal RawNum FindGcd(RawNum left, RawNum right) { if (left.IsZero()) { RawNum gcd = OmgPool.GetRawZero(); gcd.CopyFrom(right); return(gcd); } var divModRes = OmgOp.DivMod(right.OmgWrapper(), left.OmgWrapper()); divModRes.div.Release(); RawNum gcdRes = FindGcd(divModRes.mod.Raw, left); divModRes.mod.Release(); return(gcdRes); }