Пример #1
0
        public List <BigInteger> GetFactorBase()
        {
            //return new List<BigInteger>() {2, 17, 23, 29 };
            List <BigInteger> mod = new List <BigInteger>()
            {
                2, 3, 5, 7, 11
            };
            List <BigInteger> Res = new List <BigInteger>();

            if (AbvMath.GetLegendreSymbolValue(2, N) == 1)
            {
                Res.Add(2);
            }
            BigInteger Cur = 3;

            IterationsComleted = 0;
            IterationsTotal    = P;
            while (Cur < P)
            {
                if (FermatTest.IsPrime(Cur, mod) && (AbvMath.GetLegendreSymbolValue(N, Cur) == 1))
                {
                    Res.Add(Cur);
                    IterationsComleted++;
                }
                Cur += 2;
            }
            return(Res);
        }
Пример #2
0
        public BigInteger GetFactorBaseNext(List <BigInteger> bs)
        {
            List <BigInteger> mod = new List <BigInteger>()
            {
                2, 3, 5, 7, 11
            };
            BigInteger Cur = bs[bs.Count - 1] + 2;

            if (Cur == 4)
            {
                Cur++;
            }
            while (true)
            {
                if (FermatTest.IsPrime(Cur, mod) && (AbvMath.GetLegendreSymbolValue(N, Cur) == 1))
                {
                    bs.Add(Cur);
                    return(Cur);
                }
                Cur += 2;
            }
        }
Пример #3
0
        public void IsProbablyPrimeTest()
        {
            var testUnderTest = new FermatTest(10);

            Assert.IsTrue(testUnderTest.IsPrime(37));
        }
Пример #4
0
        public void IsProbablyCompositeTest()
        {
            var testUnderTest = new FermatTest(10);

            Assert.IsFalse(testUnderTest.IsPrime(50));
        }
Пример #5
0
        private async Task Computations()
        {
            Busy.Visibility = Visibility.Visible;

            PrimeNumberState result;

            var token = _token.Token;

            switch (_options.Type)
            {
            case PrimalityTestType.Aks:
                result = await Task.Run(() => AksTest.IsPrime(_number, token), token);

                break;

            case PrimalityTestType.Fermat:
            {
                var k = GetNumberOfIterations();
                if (k == -1)
                {
                    Busy.Visibility = Visibility.Hidden;
                    return;
                }

                result = await FermatTest.IsPrime(_number, k, token);
            }
            break;

            case PrimalityTestType.LucasLehmer:
                result = await LucasLehmerTest.IsPrime(_number, token);

                break;

            case PrimalityTestType.Pepin:
                result = await Task.Run(() => PepinTest.IsPrime(_number), token);

                break;

            case PrimalityTestType.RabinMiller:
            {
                var k = GetNumberOfIterations();
                if (k == -1)
                {
                    Busy.Visibility = Visibility.Hidden;
                    return;
                }

                result = await RabinMiller.IsPrime(_number, k, token);
            }
            break;

            case PrimalityTestType.SolovayStrassen:
            {
                if (!long.TryParse(_number.ToString(), out var num))
                {
                    MessageBox.Show("This number too big to use this method.\nPlease select another primality test.",
                                    "Result", MessageBoxButton.OK);


                    Busy.Visibility = Visibility.Hidden;
                    return;
                }

                var k = GetNumberOfIterations();
                if (k == -1)
                {
                    Busy.Visibility = Visibility.Hidden;
                    return;
                }

                result = await SolovayStrassen.IsPrime(num, k, token);

                break;
            }

            default:
            {
                Busy.Visibility = Visibility.Hidden;
                return;
            }
            }

            MessageBox.Show($"Your number is {Enum.GetName(typeof(PrimeNumberState), result)}", "Result");

            Busy.Visibility = Visibility.Hidden;
        }
Пример #6
0
        public static BigInteger[] GetAllPrimeDivisors(BigInteger N, QS qs)
        {
            List <BigInteger> answer = GetPrimeDivisiors1000(ref N);

            if (N == 1)
            {
                return(answer.ToArray());
            }
            var mod = new List <BigInteger>()
            {
                2, 3, 5, 7, 11, 13, 17, 19
            };

            if (FermatTest.IsPrime(N, mod))
            {
                answer.Add(N);
                return(answer.ToArray());
            }
            //QS qs = new QS() { N = N, A = (int)(AbvMath.GetLN(N)*1.3), BruteP = true, P=(int)(AbvMath.GetLN(N)*0.05+0.5) };
            var LN = AbvMath.GetLN(N);

            qs.N = N;
            BigInteger PStart = (int)(LN * 0.1 + 0.5);

            BigInteger[] AValues = new BigInteger[] { (int)LN };
            int          AValId  = 0;

            qs.P = PStart + 1;
            qs.A = AValues[0];
            var bs = qs.GetFactorBase();
            List <BigInteger> res = null;

            do
            {
                qs.P = qs.GetFactorBaseNext(bs);
                if (qs.P >= qs.A / 3)
                {
                    if (AValId == AValues.Length - 1)
                    {
                        answer.Add(N);
                        return(answer.ToArray());
                    }
                    qs.P = PStart;
                    qs.A = AValues[++AValId];
                    bs   = bs.Where(x => x <= qs.P).ToList();
                }
                try {
                    res = qs.GetPrimeDivisors(bs);
                }
                catch (Exception) {
                }
            } while (res == null || res.Any(x => x.IsOne));
            if (FermatTest.IsPrime(res[0], mod))
            {
                answer.Add(res[0]);
            }
            else
            {
                answer.AddRange(GetAllPrimeDivisors(res[0], qs));
            }
            if (FermatTest.IsPrime(res[1], mod))
            {
                answer.Add(res[1]);
            }
            else
            {
                answer.AddRange(GetAllPrimeDivisors(res[1], qs));
            }
            var ans = answer.ToArray();

            Array.Sort(ans);
            return(ans);
        }