4

Note:

0 and 1 are not prime numbers.

2 is the smallest prime number.

A prime number is a number that can be divided only by 1 and itself.

Solution 1: Basic O(n) C++ 7 lines
bool isPrime(int n){
    if(n < 2) return false;
    for(int i = 2; i < n; i++){
        if(n % i == 0) return false;
    }
    return true;
}
Solution 2: Optimized O(sqrt n) ★★★★★ C++ 34 lines
#include <iostream>
using namespace std;

int main() {
    int n = 5;
    bool prime = true;
    if (n < 2)
        prime = false;

    // i*i means i × i.
    // We check only up to √n.
    // Example: n = 5
    // i = 2 → 2×2 = 4 <= 5 → check 5 % 2
    // i = 3 → 3×3 = 9 > 5 → stop
    //
    // We don't need to check after √n,
    // because the factors after √n are already covered.
    //
    // Advantage: Fewer checks and faster execution.
    // Time Complexity: O(√n)
    // Space Complexity: O(1)
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            prime = false;
            break;
        }
    }

    if (prime)
        cout << "Prime";
    else
        cout << "Not Prime";
    return 0;
}