Prime Number
❓How to check a number is a prime number ?
The definition of prime number.
A prime number is a natural number greater than 1 that is not a product of two smaller natural number.
🔁 Iterator Method
bool isPrime(int n) {
if (n == 0 || n == 1) return false; // greater than 1
// check i = 2 to n-1
for (int i = 2; i < n; ++i) {
if (n % i == 0) return false;
}
return true;
}
🧠 Complexity Analysis
Kind | Complexity |
---|---|
Time | |
Space |