Problem Link - Primality Test Practice Problem in 500 to 1000 difficulty problems
Problem Statement:
Given an integer N, the task is to determine whether it is a prime number or not. A prime number is a number greater than 1 that has no positive divisors other than 1 and itself.
Approach:
- If
N≤1, return “no” because 1 is not a prime number. - Use a boolean variable
isPrimeinitialized totrue. - Iterate from
2to less thanNusing a loop. - If is divisible by any number within this range, set
isPrimetofalseand break the loop. - If
isPrimeremainstrue, print “yes”. - Otherwise, print “no”.
Complexity:
- Time Complexity:
O(N)for each numberN. This approach is simple but not efficient for large values ofN. - Space Complexity:
O(1), as no additional space is used apart from basic variables.