Question
C++ Prime Number Finder The first stage should be to write a function that will tell you if a given number is prime or not.
C++
Prime Number Finder
The first stage should be to write a function that will tell you if a given number is prime or not. Simply test the numbers from 2 up to the number to see if any of them evenly divide the number. If any do then the number is not prime. You should make sure you stop early if you discover it is not prime. And please make sure you use the correct type of loop for this (while) and don't use breaks or returns from the middle of the loop as those are poor coding practices.
The second stage is to write code that finds how many primes are in a given range. For example the number of primes in the range [10..100] is 21. And for [1,000..1,000,000] it is 78,330. Make sure your numbers come out correct for this serial test. This is simply a loop that keeps calling your prime checking first stage code.
You should also time how long it takes to determine the number. Recall that you can use System.currentTimeMillis() in Java to time your code within the code itself. In the C version, you may use the unix-level time command to do so: time ./findprime
Threading the Prime Number Finder
You are to simply break up the range of primes into smaller blocks given a number of threads. You should set the range and the number of threads as variables that are easy to change as I will have you make adjustments to them and rerun during the demo. Each thread should display the range it is working on as well as the final value it got over that range.
The threads should then all have their results added up and the final result displayed. Make sure you get the same results as you did in the serial version. Time this version as well.
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