My initial function to determine if a number was prime:
bool is_prime(int x) {
for (int y = 2; y < x; ++y) {
if (x % y == 0) {
return false;
}
}
return true;
}
This was done with difficulty O(x), as you may have had to go to x.
I learned about some optimizations and need to check my big-oh. Here is an improved program:
bool is_prime(int x)
{
if (x % 2 == 0 && x > 2) {
return false;
}
for (int y = 3; y*y <= x; y += 2) {
if (x % y == 0) {
return false;
}
}
return true;
}
Is the fact that I'm moving on now sqrt(), change it to O(sqrt(x))?
datta source
share