/// <summary> /// This is auxiliary method for project3 to generate prime numbers /// </summary> /// <returns>List<BigInteger> : prime numbers </returns> public List <BigInteger> generate_silent() { List <BigInteger> prime_numbers = new List <BigInteger>(); Parallel.For(0, Int32.MaxValue, (id, state) => { PrimeSeeker ps = new PrimeSeeker(); bool result = ps.seek(); if (result) { lock (count_key) { if (this.count == this.target) { state.Break(); } else { this.count += 1; prime_numbers.Add(ps.candidate_prime); } } } }); return(prime_numbers); }
/// <summary> /// Generates PrimeSeekers for prime numbers in parallel. /// </summary> public void generate() { sw.Start(); Parallel.For(0, Int32.MaxValue, id => { PrimeSeeker ps = new PrimeSeeker(); bool result = ps.seek(); if (result) { lock (count_key) { this.count += 1; Console.WriteLine("{0}: {1}", this.count, ps.candidate_prime); if (this.count == this.target) { sw.Stop(); Console.WriteLine("Time to Generate: {0}", sw.Elapsed); System.Environment.Exit(0); } else { Console.WriteLine(); } } } }); }