Question: 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

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 exe- cution, 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. Can the answer be in c++ language?

Example 4.2.1. Revise Example 4.2 to make it more optimal. When you

Chapter 4 The Handy, All-Purpose "for" Statement prime2.cpp #include #1 nclude using namespace std; int mainO int n 0; // Number to test for prime-ness bool is_prime true; // Boolean flag; assume true // until proven otherwise // Get a number from the keyboard cout

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!