There are three piles of pens. A pens in the first pile and B pens in the second Pile.Find the minimum number of pens that should be there in the third pile so that sum of all three piles produces either a prime number or unity. 
Note: there should be atleast one pen in the third pile.
Example 1:
Input: A = 1, B = 3
Output: 1
Explanation: A + B + K = prime
K = 1, 1 + 3 + 1 = 5.So answer = 1
becuase 5 is minimum possible prime. 
Example 2:
Input: A = 4, B = 3
Output: 4
Explanation: A + B + K = prime
K = 4, 4 + 3 + 4 = 11.So answer = 4
becuase 11 is minimum possible prime.
Your Task:  
You dont need to read input or print anything. Complete the function minThirdPiles() which takes A and B as input parameter and returns the the minimum number of pens that should be there in the third pile so that sum of all three piles produces a prime number.
Expected Time Complexity: O(nlogn)
Expected Auxiliary Space: O(1)
Constraints:
1 <= A <=1000
1 <= B <=1000

Difficulty: EASY
Tags: Number theory
Expected Time Complexity: O(nlogn)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3
class Solution:
	def minThirdPiles(self, A, B):
		# code here


Solutions:
class Solution:

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

	def minThirdPiles(self, A, B):
		i = 1
		flag = True
		while flag:
			p = A + B + i
			if self.isPrime(p):
				flag = False
			else:
				i += 1
		return i


class Solution:

	def isPrime(self, num):
		i = 2
		while i * i <= num:
			if num % i == 0:
				return False
				break
			i += 1
		return True

	def minThirdPiles(self, A, B):
		summ = A + B
		n = 0
		i = summ + 1
		while i < 1000000:
			if self.isPrime(i):
				n = i
				break
			i += 1
		return n - summ


class Solution:

	def minThirdPiles(self, A, B):

		def is_prime(x):
			if x == 1:
				return False
			for i in range(2, int(x ** 0.5) + 1):
				if x % i == 0:
					return False
			return True
		ans = None
		x = A + B
		for i in range(x + 1, x * x):
			if is_prime(i):
				return i - x


import math

class Solution:

	def minThirdPiles(self, A, B):

		def isprime(n):
			if n <= 1:
				return False
			for i in range(2, int(math.sqrt(n)) + 1):
				if n % i == 0:
					return False
			return True
		for x in range(1, 10000):
			if isprime(A + B + x):
				return x


class Solution:

	def minThirdPiles(self, A, B):
		if A == 0 and B == 0:
			return 1
		num = A + B + 1
		while not self.isPrime(num):
			num += 1
		return num - (A + B)

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


class Solution:

	def minThirdPiles(self, A, B):
		if A == 0 and B == 0:
			return 1
		k = A + B
		for i in range(1, k):
			s = k + i
			t = []
			for j in range(2, s):
				if s % j == 0:
					t.append(s)
					break
			if len(t) == 0:
				return i


class Solution:

	def minThirdPiles(self, A, B):

		def p(n):
			if n == 0 or n == 1:
				return False
			s = int(n ** 0.5)
			for i in range(2, s + 1):
				if n % i == 0:
					return False
			return True
		c = A + B
		d = 0
		while True:
			c += 1
			d += 1
			if p(c):
				return d


class Solution:

	def minThirdPiles(self, A, B):

		def is_prime(n):
			i = 2
			if n < 3:
				return False
			while i * i <= n:
				if n % i == 0:
					return False
				i += 1
			return True
		n = A + B
		temp = 0
		if is_prime(n):
			temp = 1
		while not is_prime(n + temp):
			temp += 1
		return temp


class Solution:

	def minThirdPiles(self, A, B):

		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
		sumval = A + B
		for i in range(1, 1000):
			if isPrime(i + sumval) == 1:
				return i


class Solution:

	def minThirdPiles(self, A, B):
		prime = [1 for _ in range(2500)]
		prime[0] = prime[1] = 0
		for i in range(2, 2500):
			if prime[i] == 1:
				for j in range(2 * i, 2500, i):
					prime[j] = 0
		for j in range(A + B + 1, 2500):
			if prime[j] == 1:
				return j - (A + B)


from math import *

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

class Solution:

	def minThirdPiles(self, A, B):
		c = A + B
		if isprime(c):
			c = c + 1
			i = 1
			while not isprime(c):
				i = i + 1
				c = c + 1
			return i
		else:
			i = 0
			while not isprime(c + i):
				i = i + 1
			return i


class Solution:

	def is_prime(self, num):
		for j in range(2, int(num ** (1 / 2)) + 1):
			if num % j == 0:
				return 0
		return 1

	def minThirdPiles(self, A, B):
		n = 1
		while True:
			pri = self.is_prime(A + B + n)
			if pri == 1:
				break
			n += 1
		if A == B:
			return min(A, n)
		return n


class Solution:

	def is_prime(self, num):
		t = num // 2 + 1
		while t > 2:
			if num % t == 0:
				return 0
			t -= 1
		return 1

	def minThirdPiles(self, A, B):
		n = 1
		while True:
			pri = self.is_prime(A + B + n)
			if pri == 1:
				break
			n += 1
		if A == B:
			return min(A, n)
		return n


import math as m

class Solution:

	def minThirdPiles(self, A, B):

		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
		z = 1
		while True:
			if pr(A + B + z):
				return z
			z = z + 1


class Solution:

	def minThirdPiles(self, A, B):
		a = 0
		p = 0
		import math
		while p == 0:
			a = a + 1
			sum = A + B + a
			for i in range(2, int(math.sqrt(sum)) + 1):
				if sum % i == 0:
					p = 1
			if p == 0:
				return a
			p = 0


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 minThirdPiles(self, A, B):
		for i in range(1, A * B):
			if prime(A + B + i) == 1:
				return i


class Solution:

	def minThirdPiles(self, A, B):
		n = A + B
		while n > 0:
			for i in range(2, int(n ** 0.5) + 2):
				if (n + 1) % i == 0:
					n += 1
					break
			else:
				return n - (A + B) + 1


class Solution:

	def minThirdPiles(self, A, B):

		def check_prime(n):
			if n == 2:
				return True
			if n <= 1 or n % 2 == 0:
				return False
			else:
				for i in range(3, n):
					if n % i == 0:
						return False
				return True
		for i in range(1, 2000):
			if check_prime(i + A + B):
				return i