Check if a number is prime or not

Beginner Problems Basic Recursion Easy
  • Prime number checking, the underlying concept of this problem, is a crucial component in cryptography, particularly RSA algorithm, which is widely used in secure data transmission
  • The security of RSA is based on the practical difficulty of factoring the product of two large prime numbers, the factoring problem

Given an integer num, return true if it is prime otherwise false.


A prime number is a number that is divisible only by 1 and itself.

Examples:

Input : num = 5

Output : true

Explanation : The factors of 5 are 1 and 5 only.

So it satisfies the prime number condition.

Input : num = 15

Output : false

Explanation : The factors of 15 are 1, 3, 5, 15 only.

As the number has factors other than 1 and itself, So it is not a prime number.

Input : num = 41

Constraints

  • 1 <= num <= 104

Company Tags

TCS Cognizant Accenture Infosys Capgemini Wipro IBM HCL Tech Mahindra MindTree