public void NaivePrimeTest()
        {
            NaivePrimeChecker naivePrimeChecker = new NaivePrimeChecker();

            TestPrimeChecker(naivePrimeChecker);
            Assert.Pass();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            // Initialize Standard configuration
            IOrderFinder  of = new QuantumOrderFinder();
            IPrimeChecker pc = new MillerRabin();

            int    n        = args.Length;
            bool   saveFile = false;
            string logfile  = "";

            // Read Arguments from the command line
            for (int i = 0; i < n; i++)
            {
                if (args[i] == "-of")
                {
                    if (i == n - 1)
                    {
                        Console.WriteLine("Error: Argument -of must be followed by the name of the OrderFinder.");
                        return;
                    }
                    string nofn = args[i + 1];
                    i++;
                    if (nofn.ToLower() == "naive")
                    {
                        of = new NaiveOrderFinder();
                    }
                    if (nofn.ToLower() == "babygiant")
                    {
                        of = new BabyGiantOrderFinder();
                    }
                    if (nofn.ToLower() == "quantum")
                    {
                        of = new QuantumOrderFinder();
                    }
                }
                if (args[i] == "-pc")
                {
                    if (i == n - 1)
                    {
                        Console.WriteLine("Error: Argument -pc must be followed by the name of the PrimeChecker.");
                        return;
                    }
                    string nofn = args[i + 1];
                    i++;
                    if (nofn.ToLower() == "naive")
                    {
                        pc = new NaivePrimeChecker();
                    }
                    if (nofn.ToLower() == "millerrabin")
                    {
                        if (i == n - 1)
                        {
                            Console.WriteLine("Error: MillerRabin must be followed by the number of iterations.");
                            return;
                        }
                        string vl = args[i + 1];
                        i++;
                        int it;
                        if (!int.TryParse(vl, out it))
                        {
                            Console.WriteLine("Error: MillerRabin must be followed by the number of iterations.");
                            return;
                        }
                        pc = new MillerRabin(it);
                    }
                }
                if (args[i] == "-h")
                {
                    Console.WriteLine(help_text);
                    return;
                }
                if (args[i] == "-f")
                {
                    if (i == n - 1)
                    {
                        Console.WriteLine("Error: -f Must be followed by valid file name");
                        return;
                    }
                    string fn = args[i + 1];
                    i++;
                    saveFile = true;
                    logfile  = Path.GetFullPath(fn);
                }
            }

            // Initialize the Timer
            PerformanceTimer.Init(pc, of);

            // Factorize each number in the input:
            // For each line, while not at end of line
            string st;

            while ((st = Console.ReadLine()) != null)
            {
                List <string> vals = st.Split(' ').ToList();
                List <long>   inp  = new List <long>();

                // Try to convert each word on this line to a number
                foreach (string s in vals)
                {
                    long r = 0;
                    if (!long.TryParse(s, out r))
                    {
                        Console.WriteLine("Error: Conversion to long failed");
                        return;
                    }
                    inp.Add(r);
                }

                // Measure the time for each of these numbers
                foreach (long v in inp)
                {
                    TimeSpan ts      = PerformanceTimer.Measure(v);
                    string   message = "Factorized " + v.ToString() + " in " + ts.Days + "d " + ts.Hours + "h " + ts.Minutes + "m " + ts.Seconds + "s " + ts.Milliseconds + "ms.";
                    if (saveFile)
                    {
                        var sw = new StreamWriter(logfile, true);
                        sw.WriteLine(message);
                        sw.Close();
                    }
                    Console.WriteLine(message);
                }
            }
        }