Example #1
0
        public static mpz_t MersennePrime(mpz_t n, ulong exponent, mpz_t mersenneNumber)
        {
            mpz_t s = n;

            for (; s.BitLength > (int)exponent; s = (s & mersenneNumber) + (s >> (int)exponent))
            {
                ;
            }
            return(s == mersenneNumber ? 0 : s);
        }
Example #2
0
        static void MakePrimes()
        {
            var primesList = new List <mpz_t>();

            for (mpz_t p = 2; p <= rbmax; p = p.NextPrimeGMP())
            {
                primesList.Add(p);
            }
            Primes = primesList.ToArray();
        }
Example #3
0
            public static bool Start06(ulong exponent)//3421
            {
                mpz_t mersenneNumber = mpz_t.One.ShiftLeft((int)exponent) - 1, s = 3;

                for (ulong i = 1; i < exponent; i++)
                {
                    s = (s * s) % mersenneNumber;
                }
                return(s == mersenneNumber - 3);
            }
Example #4
0
 static int smpf(mpz_t n)
 {
     for (mpz_t p = 2; ; p = p.NextPrimeGMP())
     {
         if (n % p == 0)
         {
             return((int)p);
         }
     }
 }
Example #5
0
            public static bool Start05(ulong exponent)//3313
            {
                mpz_t mersenneNumber = mpz_t.One.ShiftLeft((int)exponent) - 1, s = 4;

                for (ulong i = 2; i < exponent; i++)
                {
                    s = ((s * s) - 2) % mersenneNumber;
                }
                return(s == 0);
            }
Example #6
0
        public void Init2()
        {
            using mpz_t a = new mpz_t("222509832503450298345029835740293845720");

            mpz.init2(a, 20);

            string AsString = a.ToString();

            Assert.AreEqual("0", AsString);
        }
Example #7
0
            public static bool Start04(ulong exponent)//3441
            {
                mpz_t mersenneNumber = mpz_t.One.ShiftLeft((int)exponent) - 1, s = 3;

                for (ulong i = 1; i < exponent; i++)
                {
                    s = Mod.MersennePrime(s * s, exponent, mersenneNumber);
                }
                return(s == mersenneNumber - 3);
            }
Example #8
0
            public static bool Start01(ulong exponent)//2669
            {
                mpz_t mersenneNumber = mpz_t.One.ShiftLeft((int)exponent) - 1, s = 4;

                for (ulong i = 2; i < exponent; s = s < 2 ? mersenneNumber : s - 2, i++)
                {
                    mpir.mpz_powm(s, s, 2, mersenneNumber);
                }
                return(s == 0);
            }
Example #9
0
        public static bool Test(ulong exponent, ref ulong startI, ref mpz_t startS)
        {
            if (!Collection.Primes.IsPrime((int)exponent) || ((exponent & 3) == 3 && Collection.Primes.IsPrime(((int)exponent << 1) + 1)))
            {
                return(false);
            }
            mpz_t mersenneNumber = mpz_t.One.ShiftLeft((int)exponent) - 1;

            return(TrialDivision(exponent, mersenneNumber) && LucasLehmerTest.Fastest(exponent, mersenneNumber, ref startI, ref startS));
        }
Example #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="mpq_t"/> class.
        /// </summary>
        /// <param name="numerator">The numerator.</param>
        /// <param name="denominator">The denominator.</param>
        /// <param name="canonicalize">True if the new instance should use canonical numerator and denominator.</param>
        public mpq_t(mpz_t numerator, mpz_t denominator, bool canonicalize = false)
        {
            mpq_init(ref Value);
            mpq_set_num(ref Value, ref numerator.Value);
            mpq_set_den(ref Value, ref denominator.Value);

            if (canonicalize)
            {
                mpq_canonicalize(ref Value);
            }
        }
Example #11
0
        public mpz_t RandomZStarN(mpz_t n)
        {
            mpz_t r;

            do
            {
                r = GetRandomNumber();
            }while (r.CompareTo(n) >= 0 || mpz_t.Gcd(r, n) != 1);

            return(r);
        }
Example #12
0
        /// <summary>
        /// Return a random integer in Z*_{n^2}
        /// </summary>
        public mpz_t RandomZStarNSquare(mpz_t nsquare)
        {
            mpz_t r;

            do
            {
                r = GetRandomNumber();
            }while (r.CompareTo(nsquare) >= 0 || mpz_t.Gcd(r, nsquare) != 1);

            return(r);
        }
Example #13
0
 static void Bar()
 {
     // [using-sample]
     using (mpz_t a = new mpz_t(12345678901234567890))
         using (mpz_t b = new mpz_t(9876543210987654321))
             using (mpz_t c = a * b)
             {
                 System.Console.WriteLine("{0}", c.ToString());
             }
     // [/using-sample]
 }
Example #14
0
            //1,3
            public static bool Start11(ulong exponent, ref ulong startI, ref mpz_t startS)//3969
            {
                mpz_t mersenneNumber = mpz_t.One.ShiftLeft((int)exponent) - 1, s = startS, e = mpz_t.One.ShiftLeft((int)(exponent - startI));

                if (s < mersenneNumber)
                {
                    startI++;
                    startS *= startS;
                }
                return(s.PowerMod(e, mersenneNumber) == mersenneNumber - 3);
            }
Example #15
0
        public void Out()
        {
            string AsString;

            using mpz_t a = new mpz_t("622288097498926496141095869268883999563096063592498055290461");
            AsString      = a.ToString();
            Assert.AreEqual("622288097498926496141095869268883999563096063592498055290461", AsString);

            using (MemoryStream Stream = new MemoryStream())
            {
                using (StreamWriter Writer = new StreamWriter(Stream, default, -1, leaveOpen: true))
Example #16
0
        public void PerfectPower()
        {
            string AsString;

            using mpz_t a = new mpz_t("936995247845762439229916837840025004943963737679442023171062337518037547173883302933966320549");
            AsString      = a.ToString();
            Assert.AreEqual("936995247845762439229916837840025004943963737679442023171062337518037547173883302933966320549", AsString);

            bool IsPerfectPower = a.IsPerfectPower();

            Assert.IsTrue(IsPerfectPower);
        }
Example #17
0
 static int NumPrimes(int a, int b)
 {
     for (int n = 0; ; n++)
     {
         mpz_t bign = new mpz_t(n);
         mpz_t p    = bign.Power(2) + bign.Multiply(a) + b;
         if (!p.IsProbablyPrimeRabinMiller(10))
         {
             return(n);
         }
     }
 }
Example #18
0
        public void PopCount()
        {
            string AsString;

            using mpz_t a = new mpz_t("10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF", 16);
            AsString      = a.ToString(16).ToUpper();
            Assert.AreEqual("10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF", AsString);

            ulong Count = mpz.popcount(a);

            Assert.AreEqual(Count, 97UL);
        }
Example #19
0
        public void PerfectSquare()
        {
            string AsString;

            using mpz_t a = new mpz_t("95754294066634670780206802290671646680930214410991689632113801");
            AsString      = a.ToString();
            Assert.AreEqual("95754294066634670780206802290671646680930214410991689632113801", AsString);

            bool IsPerfectSquare = a.IsPerfectSquare();

            Assert.IsTrue(IsPerfectSquare);
        }
Example #20
0
        public void Sign()
        {
            string AsString;
            bool   IsNegative;

            using mpz_t a = new mpz_t("-622288097498926496141095869268883999563096063592498055290461");
            AsString      = a.ToString();
            Assert.AreEqual("-622288097498926496141095869268883999563096063592498055290461", AsString);

            IsNegative = mpz.sgn(a) < 0;
            Assert.IsTrue(IsNegative);
        }
Example #21
0
        public void AddNonCanonicOperator()
        {
            string AsString;

            using mpz_t n0 = new mpz_t("222509832503450298345029835740293845720");
            using mpz_t d0 = new mpz_t("115756986668303657898962467957");

            using mpq_t a = new mpq_t(n0, d0);
            AsString      = a.ToString();
            Assert.AreEqual("222509832503450298345029835740293845720/115756986668303657898962467957", AsString);

            using mpz_t n1 = new mpz_t("222987435987982730594288574029879874539");
            using mpz_t d1 = new mpz_t("590872612825179551336102196593");

            using mpq_t b = new mpq_t(n1, d1);
            AsString      = b.ToString();
            Assert.AreEqual("222987435987982730594288574029879874539/590872612825179551336102196593", AsString);

            using mpz_t n2 = n0 * 2;
            using mpz_t d2 = d0 * 17;

            using mpq_t c = new mpq_t(n2, d2);
            AsString      = c.ToString();
            Assert.AreEqual("445019665006900596690059671480587691440/1967868773361162184282361955269", AsString);

            c.Canonicalize();

            AsString = c.ToString();
            Assert.AreEqual("445019665006900596690059671480587691440/1967868773361162184282361955269", AsString);

            using mpz_t n3 = n1 * 2;
            using mpz_t d3 = d1 * 17;

            using mpq_t d = new mpq_t(n3, d3);
            AsString      = d.ToString();
            Assert.AreEqual("445974871975965461188577148059759749078/10044834418028052372713737342081", AsString);

            d.Canonicalize();

            AsString = d.ToString();
            Assert.AreEqual("445974871975965461188577148059759749078/10044834418028052372713737342081", AsString);

            using mpq_t e = c + d;

            AsString = e.ToString();
            Assert.AreEqual("314574639530933745596599719402044828752321603866779219489192838557566/1162759763812990990673034143436978981948983216731205310198517", AsString);

            using mpz_t Numerator = c.GetNumerator() * d.GetDenominator() + d.GetNumerator() * c.GetDenominator();

            AsString = Numerator.ToString();
            Assert.AreNotEqual("314574639530933745596599719402044828752321603866779219489192838557566", AsString);
        }
Example #22
0
        public void DivNonCanonicOperator()
        {
            string AsString;

            using mpz_t n0 = new mpz_t("222509832503450298345029835740293845720");
            using mpz_t d0 = new mpz_t("115756986668303657898962467957");

            using mpq_t a = new mpq_t(n0, d0);
            AsString      = a.ToString();
            Assert.AreEqual("222509832503450298345029835740293845720/115756986668303657898962467957", AsString);

            using mpz_t n1 = new mpz_t("590872612825179551336102196593");
            using mpz_t d1 = new mpz_t("222987435987982730594288574029879874539");

            using mpq_t b = new mpq_t(n1, d1);
            AsString      = b.ToString();
            Assert.AreEqual("590872612825179551336102196593/222987435987982730594288574029879874539", AsString);

            using mpz_t n2 = n0 * 2;
            using mpz_t d2 = d0 * 17;

            using mpq_t c = new mpq_t(n2, d2);
            AsString      = c.ToString();
            Assert.AreEqual("445019665006900596690059671480587691440/1967868773361162184282361955269", AsString);

            c.Canonicalize();

            AsString = c.ToString();
            Assert.AreEqual("445019665006900596690059671480587691440/1967868773361162184282361955269", AsString);

            using mpz_t n3 = n1 * 2;
            using mpz_t d3 = d1 * 17;

            using mpq_t d = new mpq_t(n3, d3);
            AsString      = d.ToString();
            Assert.AreEqual("1181745225650359102672204393186/3790786411795706420102905758507957867163", AsString);

            d.Canonicalize();

            AsString = d.ToString();
            Assert.AreEqual("1181745225650359102672204393186/3790786411795706420102905758507957867163", AsString);

            using mpq_t e = c / d;

            AsString = e.ToString();
            Assert.AreEqual("49616897032059882578779559314539403611774953165369490340046561577107422123080/68397633165470058274884361378645822467587248043012077070501", AsString);

            using mpz_t Numerator = c.GetNumerator() * d.GetDenominator();

            AsString = Numerator.ToString();
            Assert.AreNotEqual("49616897032059882578779559314539403611774953165369490340046561577107422123080", AsString);
        }
Example #23
0
 public FactorPair(mpz_t p1, mpz_t p2)
 {
     if (p1 > p2)
     {
         Lower  = p2;
         Higher = p1;
     }
     else
     {
         Lower  = p1;
         Higher = p2;
     }
 }
Example #24
0
        public void BasicRoot()
        {
            string AsString;

            using mpz_t a = new mpz_t("936995247845762439229916837840025004943963737679442023171062337518037547173883302933966320549");
            AsString      = a.ToString();
            Assert.AreEqual("936995247845762439229916837840025004943963737679442023171062337518037547173883302933966320549", AsString);

            mpz_t b = a.NthRoot(3);

            AsString = b.ToString();
            Assert.AreEqual("9785412309485720938412983404349", AsString);
        }
Example #25
0
        public void NegateOperator()
        {
            string AsString;

            using mpz_t a = new mpz_t("222987435987982730594288574029879874539");
            AsString      = a.ToString();
            Assert.AreEqual("222987435987982730594288574029879874539", AsString);

            using mpz_t b = -a;

            AsString = b.ToString();
            Assert.AreEqual("-222987435987982730594288574029879874539", AsString);
        }
Example #26
0
        public void Sqrt()
        {
            string AsString;

            using mpz_t a = new mpz_t("95754294066634670780206802290671646680930214410991689632113801");
            AsString      = a.ToString();
            Assert.AreEqual("95754294066634670780206802290671646680930214410991689632113801", AsString);

            mpz_t b = a.Sqrt();

            AsString = b.ToString();
            Assert.AreEqual("9785412309485720938412983404349", AsString);
        }
Example #27
0
        public void AddUint()
        {
            string AsString;

            using mpz_t a = new mpz_t("222509832503450298345029835740293845720");
            AsString      = a.ToString();
            Assert.AreEqual("222509832503450298345029835740293845720", AsString);

            using mpz_t b = new();
            mpz.add_ui(b, a, 1);
            AsString = b.ToString();
            Assert.AreEqual("222509832503450298345029835740293845721", AsString);
        }
Example #28
0
        public void AddIntOperator3()
        {
            string AsString;

            using mpz_t a = new mpz_t("222509832503450298345029835740293845720");
            AsString      = a.ToString();
            Assert.AreEqual("222509832503450298345029835740293845720", AsString);

            int MinusOne = -1;

            using mpz_t b = MinusOne + a;
            AsString      = b.ToString();
            Assert.AreEqual("222509832503450298345029835740293845719", AsString);
        }
Example #29
0
        public void AddIntOperator2()
        {
            string AsString;

            using mpz_t a = new mpz_t("222509832503450298345029835740293845720");
            AsString      = a.ToString();
            Assert.AreEqual("222509832503450298345029835740293845720", AsString);

            int Two = 2;

            using mpz_t b = a + Two;
            AsString      = b.ToString();
            Assert.AreEqual("222509832503450298345029835740293845722", AsString);
        }
Example #30
0
        public void AddUIntOperator1()
        {
            string AsString;

            using mpz_t a = new mpz_t("222509832503450298345029835740293845720");
            AsString      = a.ToString();
            Assert.AreEqual("222509832503450298345029835740293845720", AsString);

            uint One = 1;

            using mpz_t b = One + a;
            AsString      = b.ToString();
            Assert.AreEqual("222509832503450298345029835740293845721", AsString);
        }