예제 #1
0
 public static void ThreadAndLambda()
 {
     using (Benchmark b = new Benchmark("Using threads via lambdas"))
     {
         Thread t1 = new Thread(() =>
         {
             Eratosthenes eratosthenes = new Eratosthenes();
             Console.WriteLine($"Task 1 starting {DateTime.Now.ToShortTimeString()}");
             Console.WriteLine(Primes.GetPrimeFactors(13187259, eratosthenes).PrettyPrint());
             Console.WriteLine($"Task 1 ending {DateTime.Now.ToShortTimeString()}");
         }
                                );
         Thread t2 = new Thread(() =>
         {
             Eratosthenes eratosthenes = new Eratosthenes();
             Console.WriteLine($"Task 2 starting {DateTime.Now.ToShortTimeString()}");
             Console.WriteLine(Primes.GetPrimeFactors(41724259, eratosthenes).PrettyPrint());
             Console.WriteLine($"Task 2 ending {DateTime.Now.ToShortTimeString()}");
         }
                                );
         t1.Start();
         t2.Start();
         t1.Join();
         t2.Join();
     }
 }
예제 #2
0
        private static int GetAmountDivisors(long number)
        {
            IEnumerator <long> primeFactors = Primes.GetPrimeFactors(number)
                                              .GetEnumerator();

            int amountDivisors = 0;

            if (primeFactors.MoveNext())
            {
                amountDivisors++;

                int  amountCurrentFactors = 1;
                long currentFactor        = primeFactors.Current;
                while (primeFactors.MoveNext())
                {
                    long primeFactor = primeFactors.Current;
                    if (primeFactor == currentFactor)
                    {
                        amountCurrentFactors++;
                    }
                    else
                    {
                        amountDivisors      *= amountCurrentFactors + 1;
                        amountCurrentFactors = 1;
                        currentFactor        = primeFactor;
                    }
                }

                amountDivisors *= amountCurrentFactors + 1;
            }

            return(amountDivisors);
        }
예제 #3
0
        public void TestForExample()
        {
            byte[] expectedFactors = new byte[] { 5, 7, 13, 29 };

            IEnumerable <long> primeFactors = Primes.GetPrimeFactors(13195);

            Assert.AreEqual(expectedFactors, primeFactors);
        }
예제 #4
0
 static void Task2()
 {
     using (Benchmark b = new Benchmark("Task 2 prime factor find"))
     {
         Eratosthenes eratosthenes = new Eratosthenes();
         Console.WriteLine($"Task 2 starting {DateTime.Now.ToShortTimeString()}");
         Console.WriteLine(Primes.GetPrimeFactors(13187259, eratosthenes).PrettyPrint());
         Console.WriteLine($"Task 2 ending {DateTime.Now.ToShortTimeString()}");
     }
 }
예제 #5
0
        public override long Solution()
        {
            IEnumerator <long> primeFactor = Primes.GetPrimeFactors(600851475143)
                                             .GetEnumerator();

            while (primeFactor.MoveNext())
            {
            }

            return(primeFactor.Current);
        }
예제 #6
0
        public static void TaskReturnValues()
        {
            using (Benchmark b = new Benchmark("Returning values from tasks"))
            {
                Task <string> task = Task.Run(() =>
                {
                    Eratosthenes eratosthenes = new Eratosthenes();
                    return(Primes.GetPrimeFactors(13187259, eratosthenes).PrettyPrint());
                });

                Task <string> task2 = Task.Run(() =>
                {
                    Eratosthenes eratosthenes = new Eratosthenes();
                    return(Primes.GetPrimeFactors(41724259, eratosthenes).PrettyPrint());
                });
                Console.WriteLine(task.Result);
                Console.WriteLine(task2.Result);
            }
        }