patterncsharpMajor
Calculation of prime numbers making use of Parallel.ForEach
Viewed 0 times
calculationmakingnumbersparallelforeachuseprime
Problem
In my spare time I decided to write a program that would systematically identify prime numbers from 2 to 18,446,744,073,709,551,615. This is for fun and learning, as I know it will take too long to actually ever reach the upward value, but I'm using this to explore parallel processing. I know this is not a traditional question but I very much would like the critique of my peers. I know this can been torn apart, so please do, but if you do, do so constructively.
The program is designed to run until the user hits the esc key; at which time it will generate a file with all of the prime numbers discovered. The path to this file needs to be configured to a value for your directory structure. When I restart the program it will accept, as an argument, a primes text file, reading it in and starting from where it left off. The parallel processing portion and implementing the sieve for finding primes is what I was interested in woodshedding.
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace Prime
{
public class Program
{
public static void Main(string[] args)
{
List primes = new List();
primes.Add(2);
UInt64 numberToCheck = 3;
if (args.Count() > 0)
{
numberToCheck = ReadPrimesToList(args[0].ToString(), out primes) +2;
}
try
{
bool quit = false;
Console.WriteLine("Prime Number Search");
while (!quit)
{
if (Console.KeyAvailable)
quit = Console.ReadKey().Key == ConsoleKey.Escape;
Console.Write("Processing: " + numberToCheck);
if (CheckForPrime(numberToCheck, primes))
{
primes.Add(numberToCheck);
Console.WriteLine(" Prime F
The program is designed to run until the user hits the esc key; at which time it will generate a file with all of the prime numbers discovered. The path to this file needs to be configured to a value for your directory structure. When I restart the program it will accept, as an argument, a primes text file, reading it in and starting from where it left off. The parallel processing portion and implementing the sieve for finding primes is what I was interested in woodshedding.
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace Prime
{
public class Program
{
public static void Main(string[] args)
{
List primes = new List();
primes.Add(2);
UInt64 numberToCheck = 3;
if (args.Count() > 0)
{
numberToCheck = ReadPrimesToList(args[0].ToString(), out primes) +2;
}
try
{
bool quit = false;
Console.WriteLine("Prime Number Search");
while (!quit)
{
if (Console.KeyAvailable)
quit = Console.ReadKey().Key == ConsoleKey.Escape;
Console.Write("Processing: " + numberToCheck);
if (CheckForPrime(numberToCheck, primes))
{
primes.Add(numberToCheck);
Console.WriteLine(" Prime F
Solution
Console.Write is horribly slow. I mean it's not that bad, but it's worse than you might think.
Try something like:
I've had many cases where updating the console less often resulted in a massive speed boost. A good rule is not to update the console more than a few times per second.
Try something like:
if((numberToCheck + 1) % 1000 == 0)
Console.Write("Processing: " + numberToCheck);I've had many cases where updating the console less often resulted in a massive speed boost. A good rule is not to update the console more than a few times per second.
Code Snippets
if((numberToCheck + 1) % 1000 == 0)
Console.Write("Processing: " + numberToCheck);Context
StackExchange Code Review Q#828, answer score: 23
Revisions (0)
No revisions yet.