Exemple #1
0
        public static int Main()
        {
            var links = new HashSet <(long p1, long p2)>();

            var primeSet = new HashSet <long>(PrimeUtils.FilePrimes);

            foreach (var prime in PrimeUtils.FilePrimes)
            {
                links.UnionWith(GetLinks(prime, primeSet));
            }

            var currPrime = primeSet.Max();

            for (var i = 0; i < 1000000; i++)
            {
                currPrime = PrimeUtils.NextPrimeBasic(currPrime);
                links.UnionWith(GetLinks(currPrime, primeSet));
                primeSet.Add(currPrime);
            }

            links = new HashSet <(long p1, long p2)>(CompressLinks(links));

            var conn4 = ConnectedSignatures4(links).OrderBy(x => x.Item1 + x.Item2 + x.Item3 + x.Item4).ToArray();
            var conn5 = ConnectedSignatures5(links, conn4).OrderBy(x => x.Item1 + x.Item2 + x.Item3 + x.Item4 + x.Item5).ToArray();

            return(0);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            //n^2 + an + b

            long       productForLongestPrimeCoefficients = 0;
            int        longestValueForN = 0;
            PrimeUtils pu    = new PrimeUtils();
            int        LIMIT = 1000;
            int        START = -1000;

            for (int coefficientA = START; coefficientA <= LIMIT; coefficientA++)
            {
                for (int coefficientB = START; coefficientB <= LIMIT; coefficientB++)
                {
                    int  n        = 0;
                    int  count    = 0;
                    long solution = 0;
                    bool endLoop  = false;
                    long product;

                    while (!endLoop)
                    {
                        solution = calculateQuadratic(n, coefficientA, coefficientB);
                        if (pu.isPrime(solution))
                        {
                            n++;
                            //Console.WriteLine(solution + " is prime");
                            count++;
                        }
                        else
                        {
                            if (coefficientA == 1 && coefficientB == 41)
                            {
                                /// Console.WriteLine("a is 1, b is 41, n is " + count);
                            }

                            product = (coefficientA) * (coefficientB);
                            if (longestValueForN < count)
                            {
                                longestValueForN = count;
                                productForLongestPrimeCoefficients = product;
                                Console.WriteLine("Found new highest length at: a = " + coefficientA + " b = " + coefficientB + " with a length of " + count + " and a product of " + product);
                            }

                            endLoop = true;
                        }
                    }
                }
            }



            Console.WriteLine("finished");
            Console.ReadLine();
        }
Exemple #3
0
        public void Test01()
        {
            // if n < 3,317,044,064,679,887,385,961,981, it is enough to test a = 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, and 41. @ wiki

            for (int c = 0; c < 10000; c++)
            {
                string s = string.Format("331704406467988738596{0:D4}", c);

                Console.WriteLine(s + " ==> " + PrimeUtils.IsPrime_M(Common.ToBigInteger(s)));
            }
        }
Exemple #4
0
        public static bool SatisfiesConjecture(long n, SortedSet <long> primes, SortedSet <long> twiceSquares)
        {
            while (primes.Max < n)
            {
                primes.Add(PrimeUtils.NextPrime(primes));
            }
            while (twiceSquares.Max < n)
            {
                twiceSquares.Add(2 * (long)Math.Pow(twiceSquares.Count, 2));
            }

            return(primes.Any(p => twiceSquares.Contains(n - p)));
        }
Exemple #5
0
        private void IncreaseBitSetCapacity(Int32 count)
        {
            Int32 size = PrimeUtils.NextPrime(count);

            if (size > _freeIdSize)
            {
                size = _freeIdSize;
            }

            BitSet newBitSet = new BitSet(size);

            newBitSet.Or(_freeIds);
            _freeIds = newBitSet;
        }
Exemple #6
0
        private void GenerateDHPrimes()
        {
            DHPrime     = PrimeUtils.GeneratePseudoPrime(DiffiePrimesBitSize, DiffieWitnesses);
            DHGenerator = PrimeUtils.GeneratePseudoPrime(DiffiePrimesBitSize, DiffieWitnesses);

            // Prime needs to be bigger than the generator.
            if (DHGenerator > DHPrime)
            {
                var temp = DHPrime;

                DHPrime     = DHGenerator;
                DHGenerator = temp;
            }
        }
Exemple #7
0
        public Boolean Initialize()
        {
            try
            {
                _freeIds = new BitSet(PrimeUtils.NextPrime(100000));
                _freeIds.Clear();
                _freeIdCount = _freeIdSize;

                foreach (UInt32 usedObjectId in ExtractUsedObjectIdTable())
                {
                    if (_exclude.Contains(usedObjectId))
                    {
                        continue;
                    }

                    Int32 objectId = (Int32)(usedObjectId - _firstId);
                    if (usedObjectId < _firstId)
                    {
#if NLOG
                        _log.Warn("{0}: Object ID {1} in DB is less than {2}", _name, usedObjectId, _firstId);
#endif
                        continue;
                    }

                    if (objectId >= _freeIds.Count)
                    {
                        IncreaseBitSetCapacity(objectId + 1);
                    }

                    _freeIds.Set(objectId);
                    Interlocked.Decrement(ref _freeIdCount);
                }

                _nextFreeId = _freeIds.NextClear(0);
#if NLOG
                _log.Info("{0} successfully initialized", _name);
#endif
            }
            catch (Exception e)
            {
#if NLOG
                _log.Error("{0} could not be initialized correctly", _name);
                _log.Error(e);
#endif
                return(false);
            }

            return(true);
        }
Exemple #8
0
        public static long Get()
        {
            var pandigit = 9;
            var prime    = 0;

            while (prime == 0)
            {
                var arr = SetUtils.LexicographicPermutations(Enumerable.Range(1, pandigit--))
                          .Select(x => x.Reverse().DigitsToNumber())
                          .Reverse()
                          .ToArray();
                prime = arr.FirstOrDefault(x => PrimeUtils.IsPrime(x));
            }

            return(prime);
        }
Exemple #9
0
        private void startBut_Click(object sender, EventArgs e)
        {
            int colSize = int.Parse(numSizeText.Text);

            countText.Clear();

            InitVals(vals, colSize);

            var task = Task.Run(() => {
                int size = PrimeUtils.CountPrimes(vals, colSize);
                return(size);
            });

            task.ContinueWith((ant) => {
                int res             = ant.Result;
                this.countText.Text = res.ToString();
                startBut.Enabled    = true;
            }, TaskScheduler.FromCurrentSynchronizationContext());
            startBut.Enabled = false;
        }
Exemple #10
0
        public static IEnumerable <long[]> Main()
        {
            var primes = new SortedSet <long> {
                2, 3, 5, 7
            };
            var primesTodo = new SortedSet <long>();
            var digits     = 2;

            while (true)
            {
                var maxForDigits = Math.Pow(10, digits);
                while (primes.Max < maxForDigits)
                {
                    var nxt = PrimeUtils.NextPrime(primes);
                    if (nxt >= maxForDigits)
                    {
                        break;
                    }
                    primes.Add(nxt);
                    primesTodo.Add(nxt);
                }

                while (primesTodo.Any())
                {
                    var todo = primesTodo.First();
                    foreach (var family in Replacements(todo))
                    {
                        var familyPrimes = family.Where(x => primes.Contains(x)).ToArray();

                        yield return(familyPrimes);

                        primesTodo.ExceptWith(familyPrimes);
                    }
                }

                digits++;
            }
        }
Exemple #11
0
        static void Main(string[] args)
        {
            HashSet <int> circularPrimes = new HashSet <int>();

            PrimeUtils pu = new PrimeUtils();

            for (int i = 0; i < 1000001; i++)
            {
                int  len        = i.ToString().Length - 1;
                int  j          = 0;
                int  num        = i;
                bool stillPrime = true;
                while (stillPrime && j <= len)
                {
                    if (pu.IsPrime(num))
                    {
                        //Console.WriteLine(num + " is prime");

                        num = RotateDigits(num);

                        j++;
                    }
                    else
                    {
                        stillPrime = false;
                    }
                }


                if (stillPrime)
                {
                    Console.WriteLine("Found circular prime: " + i);
                    circularPrimes.Add(i);
                }
            }
            Console.WriteLine("Number of Values: " + circularPrimes.Count);
            Console.ReadLine();
        }
Exemple #12
0
        public static long Main(int distinctCount)
        {
            var primes = new List <long> {
                2
            };
            var count = 0;
            var n     = 2L;

            while (count != distinctCount)
            {
                if (primes.Last() < n)
                {
                    primes.Add(PrimeUtils.NextPrime(primes));
                }
                // stop collecting factors if more than distinct count
                var primeFactors = GetPrimeFactors(n, primes).TakeWhile((x, i) => i <= distinctCount);

                count = primeFactors.Count() == distinctCount ? count + 1 : 0;
                n++;
            }

            return(n - distinctCount);
        }
        static Boolean CheckTruncPrime(int n)
        {
            PrimeUtils pu  = new PrimeUtils();
            int        num = n;

            while (num != 0)
            {
                if (!pu.IsPrime(num))
                {
                    return(false);
                }
                else
                {
                    num /= 10; //right to left truncate
                }
            }
            num = n;
            string digits = n.ToString().Substring(1); //remove leading digit

            while (digits != "")
            {
                if (!pu.IsPrime(int.Parse(digits)))
                {
                    return(false);
                }
                if (digits.Length == 1) //Checked the string in above statement.
                {
                    digits = "";
                }
                else
                {
                    digits = digits.Substring(1);
                }
            }
            return(true);
        }
Exemple #14
0
 public static long QuadraticPrimeCount(int a, int b)
 {
     return(InfiniteSet.NaturalNumbers(true).SkipWhile(n => PrimeUtils.IsPrime(n * n + a * n + b)).First());
 }
Exemple #15
0
 private void GenerateDHKeys()
 {
     DHPrivate = PrimeUtils.GeneratePseudoPrime(DiffieKeyBitSize, DiffieWitnesses);
     DHPublic  = BigInteger.ModPow(DHGenerator, DHPrivate, DHPrime);
 }