Пример #1
0
        public static int Circulars(int range)
        {
            int           count  = 0;
            HashSet <int> primes = new HashSet <int>();

            primes.UnionWith(Primes.primeRange(range));
            HashSet <string> stringprimes = new HashSet <string>();

            stringprimes.UnionWith(primes.Select <int, string>(x => x.ToString()));

            stringprimes.RemoveWhere(x => x.Contains('2') || x.Contains('4') || x.Contains('6') || x.Contains('8') || x.Contains('0'));

            foreach (string number in stringprimes)
            {
                string varnumber = number.Substring(0);
                bool   allPrime  = true;
                for (int i = 0; i < number.Length; i++)
                {
                    char c = varnumber.First <char>();
                    varnumber += c;
                    varnumber  = varnumber.Remove(0, 1);
                    if (!primes.Contains(int.Parse(varnumber)))
                    {
                        //Console.WriteLine(number);
                        allPrime = false;
                        break;
                    }
                }
                if (allPrime == true)
                {
                    count++;
                }
                allPrime = true;
            }
            return(count + 1);
        }
Пример #2
0
        public static int primes()
        {
            HashSet <int> primeList = new HashSet <int>();

            primeList.UnionWith(Primes.primeRange(10000000));
            List <string> primeStrings = Primes.primeRange(10000000).Select <int, string>(x => x.ToString()).ToList <string>();
            int           sum          = 0;
            StringBuilder reversed     = new StringBuilder();

            foreach (string s in primeStrings)
            {
                string workingstr     = string.Copy(s);
                string backworkingstr = reversed.Append(string.Copy(workingstr).Reverse <char>().ToArray <char>()).ToString();
                reversed.Clear();
                bool allPrime = true;
                //Console.WriteLine(s);
                while (workingstr.Length > 1 && backworkingstr.Length > 1)
                {
                    workingstr     = workingstr.Remove(workingstr.Length - 1);
                    backworkingstr = backworkingstr.Remove(backworkingstr.Length - 1);
                    //Console.WriteLine("{0}, {1}", workingstr, backworkingstr);
                    if (!primeList.Contains(int.Parse(workingstr)) || !primeList.Contains(int.Parse(backworkingstr)))
                    {
                        allPrime = false;
                        break;
                    }
                }
                if (allPrime == true)
                {
                    Console.WriteLine(s);
                    sum += int.Parse(s);
                }
            }

            return(sum);
        }
Пример #3
0
        static void Main(string[] args)
        {
            DateTime t = DateTime.Now;

            switch (args.Length)
            {
            case 0:
                Console.WriteLine("Bad input; no arguments");
                break;

            case 1:
                if (args[0] == "--Nonabundant-sums")
                {
                    Console.WriteLine(NonAbundSums.Sums());
                }

                else if (args[0] == "--lexo-permutations")
                {
                    Console.WriteLine(lexicographicPermutation.getMillionth());
                }

                else if (args[0] == "--digit-fifth-powers")
                {
                    Console.WriteLine(DigitFifthSums.FifthSums());
                }
                else if (args[0] == "--truncable-primes")
                {
                    Console.WriteLine(TruncablePrimes.primes());
                }
                else if (args[0] == "--champ-constant")
                {
                    Console.WriteLine(ChampernownesConstant.Product());
                }
                else if (args[0] == "--coded-triangle-numbers")
                {
                    Console.WriteLine(TriangleNumbers.CodedTriangles());
                }
                break;

            case 2:
                if (args[0] == "--n-digit-fib")
                {
                    Console.WriteLine(OneKDigitFib.NthDigitFib(int.Parse(args[1])));
                }
                else if (args[0] == "--number-spiral")
                {
                    Console.WriteLine(NumberSpirals.spiralSum(int.Parse(args[1])));
                }
                else if (args[0] == "--amicable-numbers")
                {
                    Console.WriteLine(Amicable_Numbers.Numbers(int.Parse(args[1])));
                }
                else if (args[0] == "--primes")
                {
                    foreach (int i in Primes.primeRange(int.Parse(args[1])))
                    {
                        Console.Write("{0},  ", i);
                    }
                }
                else if (args[0] == "--circular-primes")
                {
                    Console.WriteLine(CircularPrimes.Circulars(int.Parse(args[1])));
                }
                else if (args[0] == "--double-base-palindromes")
                {
                    Console.WriteLine(DoubleBasePalindrome.PalindromeSum(int.Parse(args[1])));
                }
                else if (args[0] == "--minimum-subseq-product")
                {
                    int[] Sequence = RandomSequence.RandomList(int.Parse(args[1]));
                    Console.Write("Array generated: ");
                    foreach (int i in Sequence)
                    {
                        Console.Write("{0}, ", i);
                    }
                    Console.WriteLine();
                    Console.WriteLine(Subsequence.MinimumSubSequenceProductNaive(Sequence));
                    Console.WriteLine(Subsequence.MinimumSubsequenceProduct(Sequence));
                }

                /*else if (args[0] == "--coin-sums") {
                 *  Console.WriteLine(CoinSums.Sums(int.Parse(args[1])));
                 * }*/
                break;

            case 3:
                if (args[0] == "--distinct-powers")
                {
                    Console.WriteLine(Distinct_Powers.DistinctPowers(int.Parse(args[1]), int.Parse(args[2])));
                }
                else if (args[0] == "--lattice-paths")
                {
                    Console.WriteLine(LatticePaths.numPaths(int.Parse(args[1]), int.Parse(args[2])));
                }
                break;
            }
            Console.WriteLine("Time elapsed: {0}", t - DateTime.Now);
            Console.Read();
        }