Exemplo n.º 1
0
        private void btnTestPrimalityBrowse_Click(object sender, EventArgs e)
        {
            string filename = "";

            using (OpenFileDialog fileDialog = new OpenFileDialog())
            {
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    filename = fileDialog.FileName;
                }
            }

            if (string.IsNullOrWhiteSpace(filename) || !File.Exists(filename))
            {
                return;
            }

            string[] lines = File.ReadAllLines(filename);

            int counter = 1;

            foreach (string line in lines)
            {
                BigInteger temp = BigInteger.Parse(line);

                bool isPrime = MillerRabin.IsProbablyPrime(temp, SearchDepth);

                tbOutput.AppendText(string.Format("Line #{0} : {1}{2}", counter.ToString().PadRight(4, ' '), isPrime ? "Prime" : "Composite", Environment.NewLine));

                counter++;
            }
        }
Exemplo n.º 2
0
        private void btnTestPrimality_Click(object sender, EventArgs e)
        {
            if (IsBusy)
            {
                return;
            }

            List <BigInteger> inputLines = GetInputNumbers();

            int                 searchDepth = SearchDepth;
            DateTime            startTime   = DateTime.Now;
            List <Task <bool> > taskList    = new List <Task <bool> >();

            foreach (BigInteger number in inputLines)
            {
                Task <bool> newTask = Task.Run(() => MillerRabin.IsProbablyPrime(number, searchDepth));
                taskList.Add(newTask);
            }

            IsBusy = true;
            tbOutput.Clear();

            // Thread to gather results
            new Thread(() =>
            {
                //List<Task<bool>> taskList = new List<Task<bool>>(taskLis);
                int counter = 1;
                foreach (Task <bool> tsk in taskList)
                {
                    bool result = tsk.Result;
                    WriteOutputTextboxLine(string.Format("#{0}: {1}", counter.ToString().PadRight(3, ' '), result.ToString()));
                    counter++;
                }
                this.Invoke(new MethodInvoker(() => IsBusy = false));
            }).Start();
        }