title: How can I make threads that do 1/4 of a function?
body: <p>I'm having an application that calculates the prime numbers of the user input. so if the user puts 10 in the console. Then it will show every prime number from 0 to 10. Now If I do something like 10000000 it will take a long time before it shows every prime number so I want to divide it by 4 threads, so that each thread can do 1/4 of the total number. So the first thread does from 0 till 250000, the second thread does the calculations from 250000 till 500000, etc. this is my code so far, what I'm doing with this code now is that I have 1 thread that calculates the prime of the number that the user puts in and in the end I'm making a sum of the values in the array.</p>
  <pre><code>using System;
  using System.Diagnostics;
  using System.Linq;
  using System.Threading;
  
  namespace WeekOpdr__5
  {
      internal class Program
      {
  
          static int[] deel1 = new int[10000];
  
          static int n;
          static int startNumber;
          static int secondNumber;
  
  
          static void Main(string[] args)
          {
  
              Stopwatch sw = new Stopwatch();
  
              while (true)
              {
                  Console.WriteLine(&quot;type a number in: &quot;);
                  n = Convert.ToInt32(Console.ReadLine());
                  int start1 = 2;
                  int threadCount = 4;
                  var threads = new Thread[threadCount];
                  sw.Start();
                  for (int i = 0; i &lt; threadCount; i++)
                  {
  
                      int range = n / threadCount * 2;
  
                      Console.WriteLine(range);
                      secondNumber = n / 2;
                      int start2 = start1;
                      threads[i] = new Thread(() =&gt; PrimeNumbers(startNumber, range ));
                      //threads[i] = new Thread(() =&gt; PrimeNumbers(secondNumber, n));
                      threads[i].Start();
                  }
                  for (int i = 0; i &lt; threadCount; i++)
                  {
                      threads[i].Join();
                  }
                  Console.WriteLine($&quot;The Prime Numbers between 0 and {n} are : &quot;);
                  sw.Restart();
                  ReturnN();
                  SumN();
                  long timeElapsed = sw.ElapsedMilliseconds;
                  Console.WriteLine($&quot;\nthe total time is {timeElapsed} ms&quot;);
              }
              
              
  
              static void PrimeNumbers(int startNumber, int n)
              {
  
                  int position = 0;
                  for (int i = startNumber; i &lt;= n; i++)
                  {
                      bool primeDetected = true;
  
                      for (int j = 2; j &lt;= i / 2; j++)
                      {
                          if (i % j == 0)
                          {
                              primeDetected = false;
                              break;
                          }
                      }
  
                      if (primeDetected &amp;&amp; i != 1)
                      {
                          deel1[position++] = i;
                      }
                  }
              }
  
              static void ReturnN()
              {
                  foreach (int i in deel1)
                  {
                      Console.Write($&quot;{i} &quot;);
                  }
  
              }
  
              static void SumN()
              {
                  int sum = deel1.Sum();
                  Console.WriteLine($&quot;\nDe som van de priemgetallen tussen 2 en {n}: {sum}&quot;);
              }
              
          }
      }
  }
  </code></pre>