xnite

Multithreaded Prime Number Finder

Aug 3rd, 2015
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. /*
  2. ** Project Name: PrimeTime
  3. ** Author: Robert Whitney <[email protected]>
  4. ** Description: Uses multi-threading to find prime numbers in generated chunks and then prints all primes to the screen.
  5. **    Processes about 873,190 numbers/second in blocks of 10,000 using 4 worker threads.
  6. **    Tested on Windows 8.1 x64, w/ Intel Core i7 2600 (3.40GHz) processor.
  7. **    Max integer limited to value of Int32.MaxValue (2,147,483,647)
  8. */
  9. /* Include libraries for the project */
  10. using System;
  11. using System.Threading;
  12. using System.Diagnostics;
  13.  
  14. namespace PrimeTime
  15. {
  16.     class PrimeTime
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             int i;
  21.             int initThreadCount = Process.GetCurrentProcess().Threads.Count;
  22.             bool num = int.TryParse(args[0], out i);
  23.             if( i < 0 )
  24.             {
  25.                 Console.WriteLine("Starting point must be 0 or greater!");
  26.             }
  27.             while ( i <= Int32.MaxValue && i >= 0 )
  28.             {
  29.                 if( Process.GetCurrentProcess().Threads.Count-initThreadCount <= 4 )
  30.                 {
  31.                     Thread t = new Thread(() => checkBlockForPrime(i, i + 9999));
  32.                     t.Start();
  33.                     if (Int32.MaxValue - 10000 <= i)
  34.                     {
  35.                         i = Int32.MaxValue - 10000;
  36.                     }
  37.                     else
  38.                     {
  39.                         if (i == Int32.MaxValue)
  40.                         {
  41.                             Console.WriteLine("I have reached the maximum value of an int.");
  42.                             break;
  43.                         }
  44.                         else
  45.                         {
  46.                             i = i + 10000;
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.             Console.WriteLine("Cannot process numbers larger than " + Int32.MaxValue + " and will not process numbers smaller than 0");
  52.         }
  53.  
  54.         public static void checkBlockForPrime(int start, int end)
  55.         {
  56.             int i = start;
  57.             while( i <= end )
  58.             {
  59.                 if (checkPrime(i) == true)
  60.                 {
  61.                     Console.WriteLine(i);
  62.                 }
  63.                 i = i + 1;
  64.             }
  65.         }
  66.         public static bool checkPrime(int num)
  67.         {
  68.             // Test whether the parameter is a prime number.
  69.             if ((num & 1) == 0)
  70.             {
  71.                 if (num == 2)
  72.                 {
  73.                     return true;
  74.                 }
  75.                 else
  76.                 {
  77.                     return false;
  78.                 }
  79.             }
  80.             for (int i = 3; (i * i) <= num; i += 2)
  81.             {
  82.                 if ((num % i) == 0)
  83.                 {
  84.                     return false;
  85.                 }
  86.             }
  87.             return true;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment