Problem of the Day
A new programming or logic puzzle every Mon-Fri

Threaded Math

We haven't done too much with threading here at Problemotd so let's change that. Today's objective is to create a program that spawns several random number generators. Each random number generator will generate a random number between 1 and 100 and add it to a shared variable which is initialized at 0. Once that shared variable reaches a prime number greater than 10,000 stop all threads and print out the number.

Good luck!

Permalink: http://problemotd.com/problem/threaded-math/

Comments:

  • Anonymous - 10 years, 1 month ago

    C# .Net Solution

    class Program
    {
        private static int randomTotal = 0;
        private static List<Thread> threadList = new List<Thread>();
        private static bool keeprunning = true;
    
        static void Main(string[] args)
        {
            string numThreads = "";
            do
            {
                Console.WriteLine("Please enter the number to threads you wish to run :");
                numThreads = Console.ReadLine();
            }
            while (!Regex.IsMatch(numThreads, @"^\d*$"));
    
            int threads = Convert.ToInt32(numThreads);
    
            Console.WriteLine("Please wait while we find your random prime number > 10,000");
    
            for (int i = 0; i < threads; i++)
            {
                Thread  t = new Thread(new ThreadStart(GetPrime));
                threadList.Add(t);
            }
    
            foreach(var t in threadList)
            {
                t.Start();
                t.Join();
            }
    
            Console.ReadLine();
        }
    
        private static bool IsPrime(int number)
        {
            if (number <= 1)
                return false;
    
            for (int i = 2; i * i <= number; i++)
            {
                if (number % i == 0)
                    return false;
            }
    
            return true;
        }
    
        private static void GetPrime()
        {
            do
            {
                Random rng = new Random();
                int rnd = rng.Next(1, 100);
    
                randomTotal += rnd;
    
                var totalSnapshot = randomTotal;
    
                if (totalSnapshot > 10000 && IsPrime(totalSnapshot) && keeprunning)
                {
                    keeprunning = false;
                    Console.WriteLine(totalSnapshot.ToString());  
                }
    
            }
            while (keeprunning);
        }
    }
    

    reply permalink

  • Jason Brady - 10 years, 1 month ago

    Wouldn't this start each thread and wait for it to end sequentially? Doesn't this defeat the purpose of threaded code?

    I think changing this:

      foreach(var t in threadList)
      {
        t.Start();
        t.Join();
      }
    

    to separate loops (or using LINQ) would fix this. It would start all the threads, and then, while they are all running, wait for them to finish.

      threadList.ForEach(t => t.Start());
      threadList.ForEach(t => t.Join());
    

    reply permalink

Content curated by @MaxBurstein