Exemplo n.º 1
0
        private static Task PrintPrimes(bool all = false)
        {
            var t = new Task(() =>
            {
                List <int> batch;
                do
                {
                    lock (printLock)
                    {
                        batch       = PrimeFinder.Primes(lastPrinted, 100);
                        lastPrinted = lastPrinted + batch.Count;
                    }
                    int lineCounter = lastPrinted - batch.Count();
                    foreach (var p in batch)
                    {
                        lineCounter++;
                        Console.Write(p);
                        if (lineCounter % 10 == 0)
                        {
                            Console.WriteLine();
                        }
                        else
                        {
                            Console.Write("\t");
                        }
                    }
                }while (all && (PrimeFinder.MaxFound() > batch.Last()));
            });

            t.Start();
            return(t);
        }
Exemplo n.º 2
0
        private static void factorNumber(int Number)
        {
            try
            {
                // even number done
                if (Number % 2 == 0)
                {
                    return;
                }

                int next = 2;
                int test;
                do
                {
                    while (PrimeFinder.MaxFound() * 2 < Number)
                    {
                        Thread.Sleep(1000);
                    }
                    var tests = PrimeFinder.Primes(next);
                    while (!tests.Any())
                    {
                        Thread.Sleep(500);
                        tests = PrimeFinder.Primes(next);
                    }

                    test = tests.First();
                    next = next + tests.Count();
                    for (int i = 0; i < tests.Count(); i++)
                    {
                        test = tests[i];
                        // a divisor was found
                        if (Number % test == 0)
                        {
                            return;
                        }
                    }
                }
                // only need to test the first half of the primes
                while (test * 2 <= Number);

                // no divisor found for num
                // add it to the list
                PrimeFinder.AddPrime(Number);
            }
            catch (Exception e)
            {
                Console.WriteLine((e.Message));
            }
        }
Exemplo n.º 3
0
        public static void Find(int Max = int.MaxValue)
        {
            int start = PrimeFinder.MaxFound() + 1;
            int count = Max - start;
            var range = Enumerable.Range(start, count);

            // single thread the first 100 to prevent errors
            foreach (var num in range)
            {
                if (num % 2 != 0 && !_primes.AsReadOnly().Skip(2).Any(p => (num % p) == 0))
                {
                    PrimeFinder.AddPrime(num);
                }
            }
        }