Given a number N, you have to say if it is adjacent to two prime numbers. Two numbers are adjacent if the absolute difference between them is 1.
Example 1:
Input: N = 4
Output: Y
Explaination: The adjacent numbers 3 and
 5 are both prime.
Eaxmple 2:
Input: 7
Output: N
Explaination: The numbers 6 and 8 are 
not prime.
Your Task:
You do not need to read input or print anything. Your task is to complete the function primeAdjacent() which takes N as input parameter and reutrns 1 if it has two adjacent prime numbers, otherwise, returns 0.
Expected Time Complexity: O(sqrt(N))
Expected Auxiliary Space: O(1)
Constraints:
2 ≤ N ≤ 5000

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3

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

Solutions:
class Solution:

	def is_prime(self, n):
		if n <= 1:
			return 0
		for i in range(2, n):
			if n % i == 0:
				return 0
		return 1

	def primeAdjacent(self, N):
		ans1 = self.is_prime(N + 1)
		ans2 = self.is_prime(N - 1)
		return ans1 and ans2


class Solution:

	def primeAdjacent(self, N):

		def isprime(n):
			if n == 1 or n == 0:
				return False
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		if isprime(N - 1) and isprime(N + 1):
			return 1
		return 0


class Solution:

	def primeAdjacent(self, N):

		def is_prime(n):
			i = 2
			if n < 2:
				return 0
			while i * i <= n:
				if n % i == 0:
					return False
				i += 1
			return True
		if is_prime(N - 1) and is_prime(N + 1):
			return 1
		return 0


class Solution:

	def primeAdjacent(self, N):

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


import math

class Solution:

	def prime_Check(self, n):
		if n <= 1:
			return False
		if n == 2 or n == 3:
			return True
		if n % 2 == 0 or n % 3 == 0:
			return False
		for i in range(2, math.floor(math.sqrt(n)) + 1):
			if n % i == 0:
				return False
		return True

	def primeAdjacent(self, N):
		if Solution().prime_Check(N - 1) == True and Solution().prime_Check(N + 1) == True:
			return 1
		else:
			return 0


class Solution:

	def isPrime(self, n):
		if n <= 1:
			return 0
		if n == 2 or n == 3 or n == 5:
			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

	def primeAdjacent(self, N):
		return self.isPrime(N - 1) and self.isPrime(N + 1)


def isprime(x):
	if x <= 1:
		return 0
	for i in range(2, x):
		if x % i == 0:
			return 0
	return 1

class Solution:

	def primeAdjacent(self, N):
		if isprime(N - 1) and isprime(N + 1):
			return 1
		else:
			return 0


import math as m

class Solution:

	def primeAdjacent(self, N):

		def pr(x):
			if x < 2:
				return False
			for i in range(2, int(m.sqrt(x) + 1)):
				if x % i == 0:
					return False
			return True
		if pr(N - 1) and pr(N + 1):
			return 1
		return 0


class Solution:

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


from math import sqrt

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

class Solution:

	def primeAdjacent(self, N):
		if prime(N - 1) == 1 and prime(N + 1) == 1:
			return 1
		return 0


class Solution:

	def prime(self, n):
		if n == 1:
			return 0
		for i in range(2, n // 2 + 1):
			if n % i == 0:
				return 0
		return 1

	def primeAdjacent(self, N):
		if self.prime(N + 1) and self.prime(N - 1):
			return 1
		return 0


import math

class Solution:

	def primeAdjacent(self, n):
		if n <= 2:
			return 0
		sum = 0
		flag = True
		for j in range(2, math.ceil(math.sqrt(n - 1)) + 1):
			if (n - 1) % j == 0:
				flag = False
				break
		if flag:
			sum += 1
		else:
			pass
		flag = True
		for j in range(2, math.ceil(math.sqrt(n + 1)) + 1):
			if (n + 1) % j == 0:
				flag = False
				break
		if flag:
			sum += 1
		else:
			pass
		if sum == 2:
			return 1
		else:
			return 0


class Solution:

	def primeAdjacent(self, N):
		c = []
		p = 0
		if N == 1 or N == 2 or N == 3:
			return 0
		else:
			for i in range(N - 1, N + 2):
				for j in range(2, i):
					if i % j == 0:
						break
				else:
					c.append(i)
					p += 1
		if p == 2:
			return 1
		else:
			return 0


from math import sqrt

class Solution:

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

	def primeAdjacent(self, N):
		if self.isPrime(N - 1) and self.isPrime(N + 1):
			return 1
		else:
			return 0