private void UpdateMxLarge(long x1, long x2, int[] r)
        {
            for (var l = 0; l < r.Length; l++)
            {
                var i     = r[l];
                var x     = niLarge[i];
                var sqrt  = (long)UInt128.FloorSqrt(x);
                var xover = (long)UInt128.Min((ulong)(sqrt * C3 / C4), x);
                xover = (long)(x / (x / (ulong)xover));
                var s = (Int128)0;

                var jmin = UpToOdd(Math.Max(imax / i + 1, (long)UInt128.Min((ulong)xover + 1, x / ((ulong)x2 + 1) + 1)));
                var jmax = DownToOdd((long)UInt128.Min((ulong)xover, x / (ulong)x1));
                s += JSumLarge1(x, jmin, ref jmax, x1);
                s += JSumLarge2(x, jmin, jmax, x1);

                var kmin = Math.Max(1, x1);
                var kmax = (long)UInt128.Min(x / (ulong)xover - 1, (ulong)x2);
                s += KSumLarge1Mu(x, kmin, ref kmax, x1);
                //s += KSumLarge1M(x, kmin, ref kmax, x1);
                s += KSumLarge2(x, kmin, kmax, x1);

                mx[i] -= s;
            }
        }
示例#2
0
        public UInt128 Evaluate(UInt128 n)
        {
            var xmax  = UInt128.FloorSqrt(n);
            var s     = Evaluate(n, 1, xmax);
            var xmax2 = T1(xmax);

            return(2 * s - (UInt128)xmax2 * (UInt128)xmax2);
        }
示例#3
0
 public static ulong FloorSqrt(Int128 a)
 {
     if (a.IsNegative)
     {
         throw new ArgumentException("argument must not be negative");
     }
     return(UInt128.FloorSqrt(a.v));
 }
示例#4
0
 public static ulong FloorSquareRoot(UInt128 a)
 {
     return(UInt128.FloorSqrt(a));
 }
示例#5
0
 private UInt128 YTan(UInt128 a)
 {
     return(T1(UInt128.FloorSqrt((n / a))));
 }
示例#6
0
 public ulong UTan(UInt128 ab1, UInt128 abba, UInt128 ab2, UInt128 a3b3, UInt128 c1)
 {
     return((ulong)((ab1 + UInt128.FloorSqrt(UInt128.Square(abba + ab2) * n / a3b3) - (c1 << 1)) / 2));
 }
示例#7
0
//        [DataRow(uint.MaxValue, uint.MaxValue)]
//        [DataRow(ulong.MaxValue, ulong.MaxValue)]
        public void UInt128Test(ulong factorMax, ulong modulusMax)
        {
            var random = new MersenneTwister(0).Create <ulong>();

            for (int i = 0; i < 10000; i++)
            {
                var n     = random.Next(modulusMax - 1) + 1;
                var a     = random.Next(factorMax) % n;
                var b     = random.Next(factorMax) % n;
                var c     = random.Next(factorMax) % n;
                var d     = random.Next(factorMax) % n;
                var s     = (int)(b % 32);
                var value = (UInt128)0;
                Assert.AreEqual((BigInteger)a << s, (UInt128)a << s);
                Assert.AreEqual((BigInteger)a >> s, (UInt128)a >> s);
                Assert.AreEqual((BigInteger)a & b, (UInt128)a & b);
                Assert.AreEqual((BigInteger)a & b, a & (UInt128)b);
                Assert.AreEqual((BigInteger)a & b, (UInt128)a & (UInt128)b);
                Assert.AreEqual((BigInteger)a | b, (UInt128)a | b);
                Assert.AreEqual((BigInteger)a | b, a | (UInt128)b);
                Assert.AreEqual((BigInteger)a | b, (UInt128)a | (UInt128)b);
                Assert.AreEqual((BigInteger)a ^ b, (UInt128)a ^ b);
                Assert.AreEqual((BigInteger)a ^ b, a ^ (UInt128)b);
                Assert.AreEqual((BigInteger)a ^ b, (UInt128)a ^ (UInt128)b);
                if (a <= long.MaxValue)
                {
                    Assert.AreEqual(~(BigInteger)a, (long)~(UInt128)a);
                }
                Assert.AreEqual((BigInteger)a + b, (UInt128)a + b);
                Assert.AreEqual((BigInteger)a + b, a + (UInt128)b);
                Assert.AreEqual((BigInteger)a + b, (UInt128)a + (UInt128)b);
                Assert.AreEqual(((BigInteger)a * n + (BigInteger)b * n) % ((BigInteger)1 << 128), (UInt128)a * n + (UInt128)b * n);
                if (a >= b)
                {
                    Assert.AreEqual((BigInteger)a - b, (UInt128)a - b);
                    Assert.AreEqual((BigInteger)a - b, a - (UInt128)b);
                    Assert.AreEqual((BigInteger)a - b, (UInt128)a - (UInt128)b);
                    Assert.AreEqual((BigInteger)a * n - (BigInteger)b * n, (UInt128)a * n - (UInt128)b * n);
                }
                Assert.AreEqual(+(BigInteger)a, +(Int128)a);
                value = a; Assert.AreEqual((BigInteger)a + 1, ++value);
                value = a; Assert.AreEqual((BigInteger)a, value++);
                value = (UInt128)a * b; Assert.AreEqual((BigInteger)a * b + 1, ++value);
                value = (UInt128)a * b; Assert.AreEqual((BigInteger)a * b, value++);
                if (a > 0)
                {
                    value = a; Assert.AreEqual((BigInteger)a - 1, --value);
                    value = a; Assert.AreEqual((BigInteger)a, value--);
                }
                if (a > 0 && b > 0)
                {
                    value = (UInt128)a * b; Assert.AreEqual((BigInteger)a * b - 1, --value);
                    value = (UInt128)a * b; Assert.AreEqual((BigInteger)a * b, value--);
                }
                if (n <= uint.MaxValue)
                {
                    Assert.AreEqual((BigInteger)a * n, (UInt128)a * (uint)n);
                    Assert.AreEqual((BigInteger)b * n, (UInt128)b * (uint)n);
                    Assert.AreEqual((BigInteger)a * b * n % ((BigInteger)1 << 128), (UInt128)a * b * (uint)n);
                    Assert.AreEqual((BigInteger)n * a, (uint)n * (UInt128)a);
                    Assert.AreEqual((BigInteger)n * b, (uint)n * (UInt128)b);
                    Assert.AreEqual((BigInteger)n * a * b % ((BigInteger)1 << 128), (uint)n * ((UInt128)a * (UInt128)b));
                }
                Assert.AreEqual((BigInteger)a * b, a * (UInt128)b);
                Assert.AreEqual((BigInteger)a * b, (UInt128)a * b);
                Assert.AreEqual((BigInteger)a * b, a * (UInt128)b);
                Assert.AreEqual((BigInteger)a * b, (UInt128)a * (UInt128)b);
                if (b > 0)
                {
                    Assert.AreEqual((BigInteger)a % b, (UInt128)a % b);
                    Assert.AreEqual((BigInteger)a % b, a % (UInt128)b);
                    Assert.AreEqual((BigInteger)a % b, (UInt128)a % (UInt128)b);
                }
                Assert.AreEqual((BigInteger)a * b % n, (UInt128)a * b % n);
                Assert.AreEqual((BigInteger)a * b % n, a * (UInt128)b % n);
                Assert.AreEqual((BigInteger)a * b % n, (UInt128)a * (UInt128)b % (UInt128)n);
                if (c > 0 && d > 0)
                {
                    Assert.AreEqual((BigInteger)a * b / ((BigInteger)c * d), (UInt128)a * (UInt128)b / ((UInt128)c * (UInt128)d));
                    Assert.AreEqual((BigInteger)a * b % ((BigInteger)c * d), (UInt128)a * (UInt128)b % ((UInt128)c * (UInt128)d));
                }
                if (b > 0)
                {
                    Assert.AreEqual((BigInteger)a / b, (UInt128)a / b);
                    Assert.AreEqual((BigInteger)a / b, a / (UInt128)b);
                    Assert.AreEqual((BigInteger)a / b, (UInt128)a / (UInt128)b);
                }
                Assert.AreEqual((BigInteger)a * b / n, (UInt128)a * b / n);
                Assert.AreEqual((BigInteger)a * b / n, a * (UInt128)b / n);
                Assert.AreEqual((BigInteger)a * b / n, (UInt128)a * (UInt128)b / (UInt128)n);
                Assert.AreEqual((BigInteger)a < b, (UInt128)a < b);
                Assert.AreEqual((BigInteger)a < b, a < (UInt128)b);
                Assert.AreEqual((BigInteger)a < b, (UInt128)a < (UInt128)b);
                Assert.AreEqual((BigInteger)a <= b, (UInt128)a <= b);
                Assert.AreEqual((BigInteger)a <= b, a <= (UInt128)b);
                Assert.AreEqual((BigInteger)a <= b, (UInt128)a <= (UInt128)b);
                Assert.AreEqual((BigInteger)a > b, (UInt128)a > b);
                Assert.AreEqual((BigInteger)a > b, a > (UInt128)b);
                Assert.AreEqual((BigInteger)a > b, (UInt128)a > (UInt128)b);
                Assert.AreEqual((BigInteger)a >= b, (UInt128)a >= b);
                Assert.AreEqual((BigInteger)a >= b, a >= (UInt128)b);
                Assert.AreEqual((BigInteger)a >= b, (UInt128)a >= (UInt128)b);
                Assert.AreEqual((BigInteger)a == b, (UInt128)a == b);
                Assert.AreEqual((BigInteger)a == b, a == (UInt128)b);
                Assert.AreEqual((BigInteger)a == b, (UInt128)a == (UInt128)b);
                Assert.AreEqual((BigInteger)a != b, (UInt128)a != b);
                Assert.AreEqual((BigInteger)a != b, a != (UInt128)b);
                Assert.AreEqual((BigInteger)a != b, (UInt128)a != (UInt128)b);
                Assert.AreEqual((BigInteger)a * a, UInt128.Square(a));
                Assert.AreEqual(BigInteger.Abs(a), UInt128.Abs(a));
                Assert.AreEqual(BigInteger.Abs((BigInteger)a * b), UInt128.Abs((UInt128)a * b));
                Assert.AreEqual(BigInteger.Min(a, b), UInt128.Min(a, b));
                Assert.AreEqual(BigInteger.Min((BigInteger)a * n, (BigInteger)b * n), UInt128.Min((UInt128)a * n, (UInt128)b * n));
                Assert.AreEqual(BigInteger.Max(a, b), UInt128.Max(a, b));
                Assert.AreEqual(BigInteger.Max((BigInteger)a * n, (BigInteger)b * n), UInt128.Max((UInt128)a * n, (UInt128)b * n));
                for (var j = 0; j < 2; j++)
                {
                    var m         = UInt128.Abs(j == 0 ? (UInt128)a * (UInt128)b : (UInt128)n * (UInt128)n);
                    var floorsqrt = UInt128.FloorSqrt(m);
                    Assert.IsTrue((BigInteger)floorsqrt * floorsqrt <= m && (BigInteger)(floorsqrt + 1) * (floorsqrt + 1) > m);
                    var ceilingsqrt = UInt128.CeilingSqrt(m);
                    Assert.IsTrue((BigInteger)(ceilingsqrt - 1) * (ceilingsqrt - 1) < m && (BigInteger)ceilingsqrt * ceilingsqrt >= m);
                }
                for (var j = 0; j < 2; j++)
                {
                    var m         = j == 0 ? (UInt128)a * (UInt128)b : (UInt128)BigInteger.Pow((BigInteger)Math.Floor(Math.Pow((double)((BigInteger)a * b), (double)1 / 3)), 3);
                    var floorcbrt = UInt128.FloorCbrt(m);
                    Assert.IsTrue((BigInteger)floorcbrt * floorcbrt * floorcbrt <= m && (BigInteger)(floorcbrt + 1) * (floorcbrt + 1) * (floorcbrt + 1) > m);
                    var ceilingcbrt = UInt128.CeilingCbrt(m);
                    Assert.IsTrue((BigInteger)(ceilingcbrt - 1) * (ceilingcbrt - 1) * (ceilingcbrt - 1) < m && (BigInteger)ceilingcbrt * ceilingcbrt * ceilingcbrt >= m);
                }
                Assert.AreEqual(BigInteger.GreatestCommonDivisor((BigInteger)a, (BigInteger)b), UInt128.GreatestCommonDivisor((UInt128)a, (UInt128)b));
                Assert.AreEqual(BigInteger.GreatestCommonDivisor((BigInteger)a * b, (BigInteger)c * d), UInt128.GreatestCommonDivisor((UInt128)a * b, (UInt128)c * d));
                Assert.AreEqual(0, 0);
            }
        }