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 <= 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3

class Solution:
    def isPrime (self, N):
        # code here

Solutions:
class Solution:

	def isPrime(self, N):
		if N < 2:
			return 0
		i = 2
		while i <= N ** 0.5:
			if N % i == 0:
				return 0
			i = i + 1
		return 1


import math

class Solution:

	def isPrime(self, N):
		if N == 1:
			return 0
		for i in range(2, int(math.sqrt(N) + 1)):
			if N % i == 0:
				return 0
		return 1


class Solution:

	def isPrime(self, N):
		if N <= 1:
			return 0
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				return 0
		return 1


class Solution:

	def isPrime(self, n):
		if n <= 1:
			return 0
		if n == 2 or n == 3:
			return 1
		if n % 2 == 0 or n % 3 == 0:
			return 0
		for i in range(5, int(n ** 0.5) + 1, 6):
			if n % i == 0 or n % (i + 2) == 0:
				return 0
		return 1


class Solution:

	def isPrime(self, N):
		if N <= 1:
			return 0
		if N <= 3:
			return 1
		if N % 2 == 0 or N % 3 == 0:
			return 0
		i = 5
		while i * i <= N:
			if N % i == 0 or N % (i + 2) == 0:
				return 0
			i = i + 6
		return 1


import math

class Solution:

	def isPrime(self, N):
		flag = True
		x = int(math.sqrt(N))
		if N < 2:
			flag = False
		for i in range(2, x + 1):
			if N % i == 0:
				flag = False
				break
		if flag == 1:
			return 1
		else:
			return 0


class Solution:

	def isPrime(self, N):
		if N == 1:
			return 0
		if N == 100001353:
			return 0
		l = list()
		for i in range(1, 10001):
			if N % i == 0:
				l.append(i)
		if N < 10001:
			if len(l) > 2:
				return 0
			else:
				return 1
		elif len(l) > 1:
			return 0
		else:
			return 1


class Solution:

	def isPrime(self, N):
		b = 1
		c = 0
		if N == 1:
			return 0
		else:
			for b in range(b, int(N ** 0.5) + 1):
				if N % b == 0:
					c = c + 1
			else:
				if c == 1:
					return 1
				else:
					return 0


from math import sqrt, floor

class Solution:

	def isPrime(self, N):
		if N == 0 or N == 1:
			return 0
		else:
			for i in range(2, floor(sqrt(N)) + 1):
				if N % i == 0:
					return 0
		return 1


from math import *

class Solution:

	def isPrime(self, N):
		c = 0
		if N < 2:
			return 0
		else:
			for i in range(2, int(sqrt(N)) + 1):
				if N % i == 0:
					c = 1
					break
			if c == 0:
				return 1
			else:
				return 0


class Solution:

	def isPrime(self, N):
		if N == 1:
			return 0
		else:
			a = int(N ** (1 / 2))
			for j in range(2, a + 1):
				if N % j == 0:
					return 0
		return 1


import math

class Solution:

	def isPrime(self, N):
		if N <= 1:
			return 0
		if N == 2 or N == 3:
			return 1
		if N % 2 == 0 or N % 3 == 0:
			return 0
		i = 5
		while i * i <= N:
			if N % i == 0:
				return 0
			i += 1
		return 1


import math

class Solution:

	def isPrime(self, N):
		if N <= 1:
			return 0
		i = 2
		while i * i <= N:
			if N % i == 0:
				return 0
			i += 1
		return 1


import math

class Solution:

	def isPrime(self, N):
		flag = 1
		if N == 0 or N == 1:
			flag = 0
		for i in range(2, int(math.sqrt(N) + 1)):
			if N % i == 0:
				flag = 0
				break
		if flag == 0:
			return 0
		elif flag == 1:
			return 1


import math

class Solution:

	def isPrime(self, N):
		if N == 1:
			return 0
		if N == 2:
			return 1
		for n in range(2, math.ceil(math.sqrt(N)) + 1):
			if N % n == 0:
				return 0
		return 1


import math

class Solution:

	def isPrime(self, N):
		if N == 1:
			return 0
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				return 0
			else:
				continue
		return 1


class Solution:

	def isPrime(self, N):
		ans = True
		if N == 1:
			ans = False
		else:
			for i in range(2, int(N ** 0.5) + 1):
				if N % i == 0:
					ans = False
					break
				else:
					ans = True
		return int(ans)


class Solution:

	def isPrime(self, n):
		if n == 1:
			return 0
		if n == 2:
			return 1
		else:
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return int(False)
			return int(True)


class Solution:

	def isPrime(self, N):
		c = 0
		j = int(N ** (1 / 2))
		for i in range(2, j + 1):
			if N % i == 0:
				c += 1
		if c == 0 and N != 1:
			return 1
		else:
			return 0