Question
Write a multithreaded solution for this problem. You are asked to find the integer in the range 1 to MAX that has the largest number
Write a multithreaded solution for this problem. You are asked to find the integer in the range 1 to MAX that has the largest number of divisors. MAX should be a fixed constant declared at the top of your code no smaller than 20,000 and hopefully something like 100,000 or 200,000 (depending upon the speed of your computer). Your program must create K threads (a fixed constant declared at the top of your code) that deal with separate ranges of integers. For example, if MAX is 1000 and K is 10, then the threads would deal with the ranges 1..100, 101..200, 201..300, etc. You can assume that K evenly divides MAX so that each of the ranges contains the same number of integers. (You might consider how to deal with the situation when K does not evenly divide MAX.) When your program concludes it must display: the values of MAX and K; all the integer(s) that have the largest number of divisors (frequently there will be several integers having the same largest number); a count of how many divisors there are; the total elapsed time for the entire computation. Test your program with several values for MAX and K to be certain that it functions properly. Discussion Here is some pseudocode for solving a portion of this problem without using threads: maxDivisors = 0 // Maximum number of divisors seen so far for N from 1 to MAX: let count be the number of divisors of N if (count > maxDivisors): maxDivisors = count // New maximum number of divisors whichInt = N // The integer that gave the maximum You would need to add pseudocode to handle the issue of remembering each of the values of N that have maxDivisors number of divisors. You might use a List for this purpose. To turn this into a multithreaded program, we have to divide the integers between 1 and MAX into groups and assign each group of integers to a thread. In its run method, each thread finds the maximum number of divisors for integers in its assigned group as well as the values of N having this many divisors. The only problem is combining the final results of all the threads. To solve this problem, you might have each thread record its results when it concludes by calling an appropriately synchronized method. As a check: For integers between 1 and 20,000 the maximum number of divisors is 80 The 2 numbers that have 80 divisors are 15,120 18,480 For integers between 1 and 200,000 the maximum number of divisors is 160 The 2 numbers that have 160 divisors are 166,320 196,560
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started