Example #1
0
        private static void GenerateRPNs()
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            if (!File.Exists(Primesfile))
            {
                //We only need to run this once
                GeneratePrimes();
            }

            Console.WriteLine("Generating all RPNs...");
            string line;
            var    reader = new StreamReader(Primesfile);
            var    rpns   = new List <string>();

            while ((line = reader.ReadLine()) != null)
            {
                string temp = line;

                foreach (char prime in line)
                {
                    int match = 0;

                    while (temp.Length > 0 && !line.Contains("0"))
                    {
                        if (PrimeTool.IsPrime(Convert.ToInt32(temp)))
                        {
                            match++;
                        }
                        temp = temp.Remove(0, 1);
                        try{
                            if (match == line.Length)
                            {
                                rpns.Add(line);
                            }
                        }
                        catch (Exception) {
                            Console.WriteLine("Oops! Exception!");
                        }
                    }
                }
            } // end while reading

            _rcount = rpns.Count;
            File.WriteAllLines(Rpnfile, rpns);
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine("RPNs generated, excution time: " + elapsedMs + "ms");
            Console.WriteLine("RPNs count " + _rcount);
            var writer = new StreamWriter(Rpncountfile);

            using (writer){
                writer.Write(_rcount);
            }
        }
Example #2
0
        private static void Calculate(string[] args)
        {
            do
            {
                string[] numbers = PrimesHash.GetAllPN();
                Console.WriteLine("Enter a number");
                input = Console.ReadLine();
                int a = Convert.ToInt32(input);

                int first = PrimesHash.GetPrime(a);

                Console.WriteLine(" PM " + first);
                string temp = input;
                //2147483648
                int match = 0;

                for (int i = 1; i < 214745; i++)
                {
                    if (PrimeTool.IsPrime(i))
                    {
                        while (temp.Length > 1 && a > 0)
                        {
                            temp = temp.Remove(0, 1);
                            a    = Convert.ToInt32(temp);
                            var ti = Convert.ToInt32(temp);
                            if (PrimeTool.IsPrime(ti))
                            {
                                Console.WriteLine("Next PM " + ti);
                                match++;
                            }


                            if (match == input.Length - 1)
                            {
                                Console.WriteLine(input + " is an RPM ");
                                input = null;
                            }
                            //Console.Write(PrimesHash.GetPrime(ti));
                        }
                    }
                }

                Console.WriteLine("Enter a number");
            } while (String.IsNullOrEmpty(input));
            Console.Read();
        }
Example #3
0
        /// <summary>
        /// Method generates prime numbers up to the specified limit and filters the Robust Prime Numbers.
        /// </summary>
        /// <param name="n">Upper limit</param>
        /// <returns>List<int>List of Integers<</returns>

        public static List <int> GenerateRpmSieveOfEratosthenes(int n)
        {
            int      limit  = ApproximateNthPrime(n);
            BitArray bits   = SieveOfEratosthenes(limit);
            var      primes = new List <int>();

            for (int i = 0, found = 0; i < limit && found < n; i++)
            {
                if (bits[i])
                {
                    if (i < 8)
                    {
                        primes.Add(i);
                        Console.WriteLine(i);
                    }
                    else
                    {
                        int length = CountDigits(i);
                        int temp   = i;

                        for (int j = length, match = 1; j > 1; j--)
                        {
                            temp /= 10;
                            int mod = SetModulo(length);
                            int mo  = temp % 10;
                            if (!(mo == 0) && PrimeTool.IsPrime(i % mod))
                            {
                                length--;
                                match++;
                            }
                            if ((CountDigits(i) == 1 || match == CountDigits(i)))
                            {
                                primes.Add(i);
                                Console.WriteLine(i);
                            }
                            found++;
                        }
                    }
                }
            }
            _rcount = primes.Count;
            Console.WriteLine("Total Count: " + _rcount);
            Lprimes = primes;
            return(primes);
        }