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(); } }
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); }
public void TestForExample() { byte[] expectedFactors = new byte[] { 5, 7, 13, 29 }; IEnumerable <long> primeFactors = Primes.GetPrimeFactors(13195); Assert.AreEqual(expectedFactors, primeFactors); }
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()}"); } }
public override long Solution() { IEnumerator <long> primeFactor = Primes.GetPrimeFactors(600851475143) .GetEnumerator(); while (primeFactor.MoveNext()) { } return(primeFactor.Current); }
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); } }