Question: Programming C++ Exercise 4.2 Example 4.2.1. Revise Example 4.2 to make it more optimal. When you consider the speed of todays microprocessors, its unlikely youll

Programming C++

Exercise 4.2

Example 4.2.1. Revise Example 4.2 to make it more optimal. When you consider the speed of todays microprocessors, its unlikely youll see a difference in execution, although if you attempt to test an extremely large number, say, more than a billion, you might see a difference. (By the way, good luck in finding a prime number in that range, if youre just looking for one by chance. Prime numbers become rarer as you get into larger values.) In any case, the following changes to code make the program more efficient for large numbers: Calculate the square root of n only once by declaring a variable square_root_of_n and determining its value before entering the for loop. This variable should be a double variable. Once a divisor of n is found, you dont need to look for any more divisors. Therefore, in the if statement inside the loop, add a break statement (breaking out of the loop) after setting is_prime to false.

 Programming C++ Exercise 4.2 Example 4.2.1. Revise Example 4.2 to make

for" Statement All-purpose Chapter 4 The Handy, prime2.cpp streams. #include

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!