Prime Number | Practice | GeeksforGeeks

For a given number N check if it is prime or not. A prime number is a number which is only divisible by 1 and itself.
 
Example 1:
Input:
N = 5
Output:
1
Explanation:
5 has 2 factors 1 and 5 only.
Example 2:
Input:
N = 25
Output:
0
Explanation:
25 has 3 factors 1, 5, 25


Your Task:
You don't need to read input or print anything. Your task is to complete the function isPrime() which takes an integer N as input parameters and returns an integer, 1 if N is a prime number or 0 otherwise.
 
Expected Time Complexity: O(sqrt(N))
Expected Space Complexity: O(1)
 
Constraints:
1 <= N <= 109
0
sarkarchitra67Premiumin 7 hours
int isPrime(int N){
        // code here
        if(N<=1){
           return 0;
       }
        for(int i=2;i*i<=N;i++){
            if(N%i==0)
            return 0;
        }
            return 1;
    }
0
sarkarchitra67
This comment was deleted.
0
rohitpandey484Premium3 days ago
what is wrong is this program
class Solution{     static int isPrime(int N){       // code here        int i;       for( i=2;i<=N-1;i++)       break;       if(i==N)       return 1;       else       return 0;   }}
-1
anmolbansal26 days ago
if(N<=1){          return 0;      }      for(int i=2;i*i<=N;i++){          if(N%i == 0){              return 0;          }      }      return 1;   }
0
nourintasnim1
This comment was deleted.
+2
sumitishere33 weeks ago
 if(N<=1){           return 0;       }       for(int i=2;i<=sqrt(N);i++){           if(N%i == 0){               return 0;           }       }       return 1;
 
 
Is there a better solution for this question?
0
kartik21gr
This comment was deleted.
0
kartik21gr1 month ago
class Solution{   static int isPrime(int N){       int c,count =0;       for(int i = 2; i<N; i++){           c=N%i;           if(c==0){               count+=1;           }       }       if(count==0){           return 1;       }else{           return 0;       }   }}
+1
adityapratyush1 month ago
Python Solution:
def isPrime (self, N):       factor = 0       num = int(N**(1/2) + 1)       if N == 1:           return 0       else:           for i in range(2, num):               if N % i == 0:                   factor += 1           if factor == 0:               return 1           else:               return 0
0
princejee20192 months ago
C++ 0(Sqrt(n)) || Easy To undstand
 int isPrime(int N){       if(N<=1) return 0;       if(N==2 || N==3) return 1;              if(N%2==0 || N%3==0)return 0;        for(int i =5;i*i<=N;i+=6){           if(N%i==0 || N%(i+2)==0){               return 0;           }       }       return 1;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.