Question: IN C++ Optimize the program by calculating the square root of n just once, rather than repeatedly. You'll need to declare another variable and set

IN C++ Optimize the program by calculating the square root of n just once, rather than repeatedly. You'll need to declare another variable and set it to the square root of n. The type should be DOUBLE. You can then use this variable in the "while" conditiion. The program is a program called prime1.cpp which will be shown below.

IN C++ Optimize the program by calculating the square root of n

Lastly, how would I optimize this shown below?

while ( i

if ( n% i == 0 ) {

is_prime = false;

}

i++;

}

prime1.cpp #include #include using namespace std; int main() { int n = 0; // Number to test for prime-ness int i = 2; // Loop counter bool is_prime = true; // Boolean flag... // Assume true for now. // Get a number from the keyboard. cout > n; // Test for prime by checking for divisibility // by all whole numbers from 2 to sqrt(n). while (i

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!