#include <iostream>
#include <cmath>
usingnamespace std;
int main(){
int n, i, is_prime = true;
cout << "Enter a number and press ENTER: ";
cin >> n;
double m = sqrt((double)n);
i = 2;
while(i <= sqrt((double)n)){
if (n % i == 0){
is_prime = false;
break;
}
i++;
}
if(is_prime)
cout << "Number is prime." << endl;
else
cout << "Number is not prime." << endl;
system("pause");
return 0;}
Exercise 2.4.1. Optimize the program by calculating the square root of n just once,
rather than over and over. 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 condition.
my question is:
if they want me to identify if its a prime number or not a prime number with just one calculation, then it would be impossible right?
i wouldnt get the correct answer wether its prime or not when i enter a random number.
my second question:
how should i use the variable m s condition?
i have been trying to but i always end up with errors :(
cant think of a way to solve this exercise feels like im lways misunderstanding exercises