PRB01 - Editorial

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 isPrime initialized to true .
  • Iterate from 2 to less than N using a loop.
  • If is divisible by any number within this range, set isPrime to false and break the loop.
  • If isPrime remains true , print “yes”.
  • Otherwise, print “no”.

Complexity:

  • Time Complexity: O(N) for each number N. This approach is simple but not efficient for large values of N.
  • Space Complexity: O(1), as no additional space is used apart from basic variables.