Program to check if given number N is Prime or not
Given a number N, the task is to check if the number is a prime number or not.
Examples:
Input: N = 11
Output: true
Explanation: The number is not divisible by any number, other than 1 and 11 itself.Input: N = 35
Output: false
Explanation: Apart from 1 and 35, this number is also divisible by 5 and 7.
Naive Approach (using recursion): To check the number is prime or not using recursion follow the below idea:
Recursion can also be used to check if a number between 2 to n – 1 divides n. If we find any number that divides, we return false.
Below is the implementation for the below idea:
C++
|
Java
|
Python3
|
C#
|
Javascript
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach: An efficient solution is :
To iterate through all numbers from 2 to sqrt(n) and for every number check if it divides n. If we find any number that divides, we return false.
Below is the implementation:
C++14
|
Java
|
Python3
|
C#
|
PHP
|
Javascript
|
Time Complexity: O(sqrt(n))
Auxiliary space: O(1)
Another Efficient Approach: To check whether the number is prime or not follow the below idea:
In the previous approach given if the size of the given number is too large then its square root will be also very large, so to deal with large size input we will deal with a few numbers such as 1, 2, 3, and the numbers which are divisible by 2 and 3 in separate cases and for remaining numbers, we will iterate our loop from 5 to sqrt(n) and check for each iteration whether that (iteration) or (that iteration + 2) divides n or not. If we find any number that divides, we return false.
Below is the implementation for the above idea:
C++
|
C
|
Java
|
Python3
|
C#
|
Javascript
|
Time complexity: O(sqrt(n))
Auxiliary space: O(1)
Given a number N, the task is to check if the number is a prime number or not.
Examples:
Input: N = 11
Output: true
Explanation: The number is not divisible by any number, other than 1 and 11 itself.Input: N = 35
Output: false
Explanation: Apart from 1 and 35, this number is also divisible by 5 and 7.
Naive Approach (using recursion): To check the number is prime or not using recursion follow the below idea:
Recursion can also be used to check if a number between 2 to n – 1 divides n. If we find any number that divides, we return false.
Below is the implementation for the below idea:
C++
|
Java
|
Python3
|
C#
|
Javascript
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach: An efficient solution is :
To iterate through all numbers from 2 to sqrt(n) and for every number check if it divides n. If we find any number that divides, we return false.
Below is the implementation:
C++14
|
Java
|
Python3
|
C#
|
PHP
|
Javascript
|
Time Complexity: O(sqrt(n))
Auxiliary space: O(1)
Another Efficient Approach: To check whether the number is prime or not follow the below idea:
In the previous approach given if the size of the given number is too large then its square root will be also very large, so to deal with large size input we will deal with a few numbers such as 1, 2, 3, and the numbers which are divisible by 2 and 3 in separate cases and for remaining numbers, we will iterate our loop from 5 to sqrt(n) and check for each iteration whether that (iteration) or (that iteration + 2) divides n or not. If we find any number that divides, we return false.
Below is the implementation for the above idea:
C++
|
C
|
Java
|
Python3
|
C#
|
Javascript
|
Time complexity: O(sqrt(n))
Auxiliary space: O(1)