예제 #1
0
        static long S(int digitLength, int targetNum)
        {
            int m = M(digitLength, targetNum);

            int[] digitsIndex = new int[digitLength];
            for (int i = 0; i < digitLength; i++)
            {
                digitsIndex[i] = i;
            }
            List <int[]> digitsGroup = CombinationProvider.BuildDistinctCombination <int>(digitsIndex, m);
            long         s           = 0;
            int          primeCount  = 0;

            foreach (int[] digits in digitsGroup)
            {
                // 逐个创建对应的数字,如果数字为素数,则返回对应的i;
                if (digits.Contains(0) && 0 == targetNum)
                {
                    continue;
                }
                NumberGenerator ng  = new NumberGenerator(digitLength, digits, targetNum);
                long            num = ng.NextNumber();
                while (num != -1)
                {
                    if (MillerRabinCheck.isPseudoPrime(num))
                    {
                        s += num;
                        primeCount++;
                    }
                    num = ng.NextNumber();
                }
            }
            return(s);
        }
예제 #2
0
 static int M(int digitLength, int targetNum)
 {
     int[] digitsIndex = new int[digitLength];
     for (int i = 0; i < digitLength; i++)
     {
         digitsIndex[i] = i;
     }
     for (int currLen = digitLength - 1; currLen > 0; currLen--)
     {
         List <int[]> digitsGroup = CombinationProvider.BuildDistinctCombination <int>(digitsIndex, currLen);
         foreach (int[] digits in digitsGroup)
         {
             // 逐个创建对应的数字,如果数字为素数,则返回对应的i;
             if (digits.Contains(0) && 0 == targetNum)
             {
                 continue;
             }
             NumberGenerator ng  = new NumberGenerator(digitLength, digits, targetNum);
             long            num = ng.NextNumber();
             while (num != -1)
             {
                 if (MillerRabinCheck.isPseudoPrime(num))
                 {
                     return(currLen);
                 }
                 num = ng.NextNumber();
             }
         }
     }
     ;
     return(0);
 }
예제 #3
0
        static void Main(string[] args)
        {
            // 创建密文的char列表
            string str = FileReader.ReadFile(@"p059_cipher.txt").Replace("\n", "");

            string[] chrAsciiStr = str.Split(',');
            char[]   chrArray    = new char[chrAsciiStr.Length];
            for (int i = chrAsciiStr.Length - 1; i >= 0; i--)
            {
                chrArray[i] = (char)int.Parse(chrAsciiStr[i]);
            }
            string source = new string(chrArray);

            // 创建可能的Key列表
            char[]        seeds      = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
            List <char[]> keys       = CombinationProvider.BuildCombination <char>(seeds, 3);
            string        decriptStr = "";

            foreach (char[] key in keys)
            {
                bool found = false;
                decriptStr = decript(chrArray, key);
                foreach (string word in normalWords)
                {
                    if (decriptStr.Contains(word))
                    {
                        found = true;
                        break;
                    }
                    if (found)
                    {
                        break;
                    }
                }
                if (found)
                {
                    break;
                }
            }
            long sum = 0;

            foreach (char chr in decriptStr)
            {
                sum += chr;
            }
            Console.WriteLine("Result is {0}", sum);
        }
예제 #4
0
        static void Main(string[] args)
        {
            CombinationProvider.Initialize(new CombinationProviderSetting_A());

            var list = new List <Combination>();

            for (int i = 0; i < 10000; i++)
            {
                var combination = CombinationProvider.Instance.CreateCombination();
                list.Add(combination);
                //Console.WriteLine(combination.ToString());
            }

            Console.WriteLine("===============================");
            new Apriori <FactionType_TwoPlayer>(0.25, 0, list.Cast <IAprioriData <FactionType_TwoPlayer> >()).Train();

            Console.WriteLine("End");
            Console.Read();
        }
예제 #5
0
        static void Main(string[] args)
        {
            int count = 0;

            for (int seedCount = 1; seedCount <= 100; seedCount++)
            {
                for (int length = 1; length <= seedCount; length++)
                {
                    bool outofmaxval = false;
                    long tmpCount    = CombinationProvider.CountCombination(seedCount, length, 10000000, out outofmaxval);
                    if (tmpCount < 0)
                    {
                        Console.WriteLine();
                    }
                    if (tmpCount > 1000000)
                    {
                        count++;
                    }
                }
            }
        }
예제 #6
0
        static void Main(string[] args)
        {
            int longest = 0;
            // 为所有单词制作签名并加入到签名相同的对应签名组
            string files = FileReader.ReadFile("p098_words.txt");

            string[] words = files.Replace("\"", "").Split(',');
            foreach (string word in words)
            {
                string sign = string.Join("", word.OrderBy(x => x));
                if (!SignedWords.ContainsKey(sign))
                {
                    SignedWords.Add(sign, new List <string>());
                }
                SignedWords[sign].Add(word);
                int length = word.Length;
                if (length > longest)
                {
                    longest = length;
                }
            }
            List <string[]> pairWords = new List <string[]>();

            // 将同一签名组的单词两两分组,创建对应的组合
            foreach (string key in SignedWords.Keys)
            {
                if (SignedWords[key].Count < 2)
                {
                    continue;
                }
                List <string[]> pairs = CombinationProvider.BuildDistinctCombination <string>(SignedWords[key].ToArray(), 2);
                pairWords.AddRange(pairs);
            }
            Dictionary <long, List <long> > pows = new Dictionary <long, List <long> >();
            Stopwatch sw = new Stopwatch();

            sw.Start();
            long m = 10;

            pows.Add(m, new List <long>());
            for (long i = 1; ; i++)
            {
                long pow = i * i;
                if (pow >= (long)100000000000000)
                {
                    break;
                }
                if (pow < m)
                {
                    pows[m].Add(pow);
                }
                else
                {
                    m *= 10;
                    pows.Add(m, new List <long>()
                    {
                        pow
                    });
                }
            }
            sw.Stop();
            Debug.WriteLine("time used {0}s{1}ms", sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
            // 计算这一组合是不是重排平方单词对
            long maxnum = 0;

            foreach (string[] pair in pairWords)
            {
                int  len = pair[0].Length;
                long key = 1;
                for (int i = 0; i < len; i++)
                {
                    key *= 10;
                }
                long[] nums = pows[key].OrderByDescending(x => x).ToArray();
                foreach (long num in nums)
                {
                    Dictionary <char, int> mapping = BuildCharactorMapping(pair[0], num);
                    if (mapping == null)
                    {
                        continue;
                    }
                    if (mapping[pair[1][0]] == 0)
                    {
                        continue;
                    }
                    long num2 = BuildNumber(pair[1], mapping);
                    if (!nums.Contains(num2))
                    {
                        continue;
                    }
                    if (maxnum < num)
                    {
                        maxnum = num;
                    }
                }
                foreach (long num in nums)
                {
                    Dictionary <char, int> mapping = BuildCharactorMapping(pair[1], num);
                    if (mapping == null)
                    {
                        continue;
                    }
                    if (mapping[pair[0][0]] == 0)
                    {
                        continue;
                    }
                    long num2 = BuildNumber(pair[0], mapping);
                    if (!nums.Contains(num2))
                    {
                        continue;
                    }
                    if (maxnum < num)
                    {
                        maxnum = num;
                    }
                }
            }
        }