Пример #1
0
        static void Main(string[] args)
        {
            int[] primes = pg.GetPrimesBelowOneMillion().Where(x => x < PRIME_LIMIT).ToArray();
            primeMark = pg.CheckPrimeNumber(PRIME_LIMIT);
            Dictionary <int, Dictionary <int, bool> > PrimeContat = new Dictionary <int, Dictionary <int, bool> >();

            #region 找出所有的素数对
            for (int i = 0; i < primes.Length; i++)
            {
                for (int j = i + 1; j < primes.Length; j++)
                {
                    if (PrimeContat.ContainsKey(primes[i]) && PrimeContat[primes[i]].ContainsKey(primes[j]))
                    {
                        continue;
                    }
                    bool isContat = CheckContact(primes[i], primes[j]);
                    if (!isContat)
                    {
                        continue;
                    }
                    if (PrimeContat.ContainsKey(primes[i]))
                    {
                        if (PrimeContat[primes[i]].ContainsKey(primes[j]))
                        {
                            continue;
                        }
                        PrimeContat[primes[i]].Add(primes[j], isContat);
                    }
                    else
                    {
                        Dictionary <int, bool> tmp = new Dictionary <int, bool>();
                        tmp.Add(primes[j], isContat);
                        PrimeContat.Add(primes[i], tmp);
                    }
                    if (PrimeContat.ContainsKey(primes[j]))
                    {
                        if (PrimeContat[primes[j]].ContainsKey(primes[i]))
                        {
                            continue;
                        }
                        PrimeContat[primes[j]].Add(primes[i], isContat);
                    }
                    else
                    {
                        Dictionary <int, bool> tmp = new Dictionary <int, bool>();
                        tmp.Add(primes[i], isContat);
                        PrimeContat.Add(primes[j], tmp);
                    }
                }
            }
            #endregion
        }
Пример #2
0
        static void Main(string[] args)
        {
            for (int i = 0; i < 10000; i++)
            {
                squares[i * i] = true;
            }
            squares[0] = false;
            PrimeGenerator pg = new PrimeGenerator();

            primes   = pg.GetPrimesBelowOneMillion();
            primeTag = pg.CheckPrimeNumber(1000000);
            int  squTwice = 0;
            int  firstOdd = 0;
            bool pass     = false;

            for (int i = 3; ; i += 2)
            {
                if (primeTag[i])
                {
                    continue;
                }
                pass = false;
                foreach (int prime in primes)
                {
                    if (i <= prime)
                    {
                        break;
                    }
                    squTwice = i - prime;
                    if (squTwice % 2 != 0)
                    {
                        continue;
                    }
                    squTwice = squTwice / 2;
                    if (!squares[squTwice])
                    {
                        continue;
                    }
                    pass = true;
                    break;
                }
                if (pass)
                {
                    continue;
                }
                firstOdd = i;
                break;
            }
            Console.WriteLine(string.Format("Result is {0}", firstOdd));
        }
Пример #3
0
        static void Main(string[] args)
        {
            primeMark = pg.CheckPrimeNumber(MAX);
            int count = 0;

            for (int i = 2; i < MAX; i++)
            {
                if (CheckCircularPrime(i))
                {
                    count++;
                    Console.WriteLine(i);
                }
            }
            Console.WriteLine(string.Format("Primes count below {0} is {1}", MAX, count));
        }
Пример #4
0
        static void Main(string[] args)
        {
            primes = pg.CheckPrimeNumber(5000000);
            List <int> result = new List <int>();
            int        sum    = 0;

            for (int i = 10; i <= 5000000; i++)
            {
                if (!Check(i))
                {
                    continue;
                }
                result.Add(i);
                sum += i;
            }
            Console.WriteLine(string.Format("Result is {0}", sum));
        }
Пример #5
0
        static void Main(string[] args)
        {
            PrimeGenerator pg = new PrimeGenerator();

            bool[] primeArray = pg.CheckPrimeNumber(2000000);
            long   sum        = 0;

            for (int i = 0; i < primeArray.Length; i++)
            {
                if (primeArray[i])
                {
                    sum += i;
                }
            }
            Console.WriteLine(sum);
            Console.Read();
        }
Пример #6
0
        static void Main(string[] args)
        {
            PrimeGenerator pg = new PrimeGenerator();

            int[]  primes    = pg.GetPrimesBelowOneMillion();
            bool[] primeMark = pg.CheckPrimeNumber(1000000);
            for (int i = 0; i < primes.Length; i++)
            {
                int   prime  = primes[i];
                int[] digits = NumberUtils.SplitNumber(prime, 1);
                for (int mark = 1; mark < ((1 << digits.Length) - 1); mark++)
                {
                    int primeCount = 0;
                    for (int n = 0; n < 10; n++)
                    {
                        int[] markedDigits = ChangeMarkedDigits(digits, mark, n);
                        if (markedDigits[0] % 2 == 0 || markedDigits[0] % 5 == 0)
                        {
                            continue;
                        }
                        if (markedDigits[digits.Length - 1] == 0)
                        {
                            continue;
                        }
                        int num = MarkDigitsToNumber(markedDigits);
                        if (!primeMark[num])
                        {
                            continue;
                        }
                        primeCount++;
                    }
                    if (primeCount == 8)
                    {
                        int result = int.MaxValue;
                        for (int n = 0; n < 10; n++)
                        {
                            int[] markedDigits = ChangeMarkedDigits(digits, mark, n);
                            if (markedDigits[0] % 2 == 0 || markedDigits[0] % 5 == 0)
                            {
                                continue;
                            }
                            if (markedDigits[digits.Length - 1] == 0)
                            {
                                continue;
                            }
                            int num = MarkDigitsToNumber(markedDigits);
                            if (!primeMark[num])
                            {
                                continue;
                            }
                            if (num < result)
                            {
                                result = num;
                            }
                        }
                        Console.WriteLine("Result is {0}", result);
                        return;
                    }
                    //Console.WriteLine(Convert.ToString(mark, 2));
                }
            }
        }