You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank you for your amazing course so far! I noticed that the for loop should be i <= sqrt(n), if not, you'd get true for numbers like 9
#include <iostream>
#include <cmath> /// We need the "cmath" library for the sqrt function
using namespace std;
bool isPrime(int n) {
if(n <= 1)
return false;
for(int i = 2; i < sqrt(n); i++) {
if(n % i == 0)
return false;
}
return true;
}
int main()
{
cout << isPrime(9);
return 0;
}
The text was updated successfully, but these errors were encountered:
You forget to apply the right operator in the loop:
#include <cmath> /// We need the "cmath" library for the sqrt function
using namespace std;
bool isPrime(int n) {
if(n <= 1)
return false;
for(int i = 2; i <= sqrt(n); i++) {
if(n % i == 0)
return false;
}
return true;
}
int main()
{
cout << isPrime(9);
return 0;
}
Thank you for your amazing course so far! I noticed that the for loop should be i <= sqrt(n), if not, you'd get true for numbers like 9
The text was updated successfully, but these errors were encountered: