Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

Primality Test

Given a number check if it is prime or not.

O(N) Approach

O(sqrt(n)) Solution

Code

bool isPrime(int n){
    if (n == 1) return false;

    for (int i = 2; i * i <= n; i++){
        if (n % i == 0) 
            return false;
    }
    return true;
}