Sixy primes are prime numbers that differ from each other by six. For example, the numbers 5 and 11 are both sixy primes, because they differ by 6. Given a range of the form [L, R]. The task is to find all the sixy prime pairs in the range in increasing order.
Example 1:
Input: L = 11, R = 19
Output: [11, 17, 13, 19] 
Explanation: There are total two pair possible
with difference 6 and these are 11,17,13,19.  
Example 2:
Input: L = 6, R = 20
Output: [7, 13, 11, 17, 13, 19]
Explanation: There are total three pair possible
with difference 6 and these are 7,13,11,17,13,19. 
Your Task:  
You dont need to read input or print anything. Complete the function sixyPrime() which takes L and R as input parameter and returns all the sixy prime exist and if there are no sixy prime exist returns -1.
Expected Time Complexity: O(nloglogn)
Expected Auxiliary Space: O(n)
Constraints:
1 <= L <= R <=10^{3}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3

class Solution:

	def sixyPrime(self, L, R):

		# code here



Solutions:
class Solution:

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

	def sixyPrime(self, L, R):
		l = []
		c = []
		for i in range(L, R + 1, 1):
			if self.isprime(i):
				l.append(i)
		for i in range(0, len(l)):
			for j in range(0, len(l)):
				if abs(l[i] - l[j]) == 6:
					if l[i] and l[j] in c:
						continue
					c.append(l[i])
					c.append(l[j])
					break
		if len(c) == 0:
			c.append(-1)
		return c


class Solution:

	def sixyPrime(self, L, R):
		n = R
		sieve = [True] * (n + 1)
		prime = []
		for i in range(2, n + 1):
			if sieve[i]:
				for j in range(i * i, n + 1, i):
					sieve[j] = False
				prime.append(i)
		res = []
		for i in range(len(prime)):
			if prime[i] < L:
				continue
			j = i + 1
			while j < len(prime):
				if prime[j] - prime[i] == 6:
					res.append(prime[i])
					res.append(prime[j])
					break
				elif prime[j] - prime[i] > 6:
					break
				else:
					j += 1
		if not res:
			return [-1]
		return res


from itertools import combinations as pp

class Solution:

	def sixyPrime(self, L, R):
		f = list(range(R + 1))
		f[0] = 0
		f[1] = 0
		p = 2
		while p < R + 1:
			if p != 0:
				for i in range(p * p, R + 1, p):
					f[i] = 0
			p += 1
		f.sort()
		a = 0
		del f[0:f.index(2)]
		for i in range(len(f)):
			if L in f:
				a = f.index(L)
				break
			elif f[i] > L:
				a = f.index(f[i])
				break
		pf = f[a:]
		sa = []
		for i in pf:
			if i + 6 in pf:
				sa.append(i)
				sa.append(i + 6)
		return sa if len(sa) != 0 else [-1]


class Solution:

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

	def sixyPrime(self, l, r):
		pl = []
		for i in range(l, r + 1):
			if self.isprime(i):
				pl.append(i)
		pairs = []
		cnt = 0
		for i in range(len(pl) - 1):
			for j in range(i + 1, len(pl)):
				if abs(pl[i] - pl[j]) == 6:
					cnt += 1
					pairs.append(pl[i])
					pairs.append(pl[j])
		if cnt:
			return pairs
		else:
			return [-1]


class Solution:

	def sixyPrime(self, L, R):
		arr = []
		for i in range(L, R - 5):
			if self.isPrime(i) and self.isPrime(i + 6):
				arr.append(i)
				arr.append(i + 6)
		if len(arr) == 0:
			arr.append(-1)
		return arr

	def isPrime(self, n):
		if n == 2 or n == 3:
			return True
		elif n == 1:
			return False
		else:
			i = 2
			while i < n:
				if n % i == 0:
					return False
				i += 1
			return True


class Solution:

	def sixyPrime(self, L, n):
		N = n
		arr = [True] * (N + 1)
		arr[0] = False
		arr[1] = False
		for i in range(2, int(n ** 0.5) + 1):
			if arr[i]:
				for j in range(i * i, N + 1, i):
					arr[j] = False
		ret = list()
		for i in range(L, n + 1 - 6):
			if arr[i] and arr[i + 6]:
				ret.append(i)
				ret.append(i + 6)
		return ret if ret else [-1]
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		(L, R) = input().split()
		L = int(L)
		R = int(R)
		ob = Solution()
		ans = ob.sixyPrime(L, R)
		for i in range(len(ans)):
			print(ans[i], end=' ')
		print()


class Solution:

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

	def sixyPrime(self, L, R):
		v = []
		c = L
		while c + 6 <= R:
			if self.prime(c) and self.prime(c + 6):
				v.append(c)
				v.append(c + 6)
			c += 1
		if len(v) == 0:
			v.append(-1)
		return v


class Solution:

	def sixyPrime(self, L, R):
		p = 2
		prime = [True for i in range(R + 1)]
		l = []
		while p * p <= R:
			if prime[p]:
				for i in range(p * p, R + 1, p):
					prime[i] = False
			p = p + 1
		for i in range(L, R - 6 + 1):
			if prime[i] and prime[i + 6]:
				l.append(i)
				l.append(i + 6)
		if l:
			return l
		else:
			return [-1]


import math

class Solution:

	def sixyPrime(self, L, R):
		lis = []
		l = 1001
		siv = [0] * l
		siv[0] = 1
		siv[1] = 1
		for i in range(2, int(math.sqrt(l)) + 1):
			if siv[i] == 0:
				for j in range(2 * i, R + 1, i):
					siv[j] = 1
		for i in range(L, R + 1):
			if siv[i] == 0:
				if siv[i] == 0 and siv[i + 6] == 0 and (i + 6 <= R):
					lis.append(i)
					lis.append(i + 6)
		if len(lis) == 0:
			lis.append(-1)
		return lis


class Solution:

	def sixyPrime(self, L, R):
		from math import sqrt
		N = R
		k = []
		if N == 1 or N == 2:
			return [-1]
		l = [1] * (N + 1)
		l[0] = 0
		l[1] = 0
		k = []
		for i in range(2, int(sqrt(N)) + 1):
			if l[i] == 1:
				for j in range(i * i, N + 1, i):
					l[j] = 0
		for i in range(L, N - 5):
			if l[i] == 1 and l[i + 6] == 1:
				k.append(i)
				k.append(i + 6)
		if len(k) == 0:
			return [-1]
		return k


class Solution:

	def sixyPrime(self, L, R):
		l = []
		arr = [1] * (R + 1)
		arr[0] = arr[1] = 0
		for i in range(2, R + 1):
			if arr[i] == 1:
				for j in range(i * i, R + 1, i):
					arr[j] = 0
		for i in range(L, R + 1):
			if arr[i] == 1 and i + 6 <= R and (arr[i + 6] == 1):
				l.append(i)
				l.append(i + 6)
		if len(l) == 0:
			return [-1]
		return l


class Solution:

	def sixyPrime(self, l, r):
		l1 = []
		for i in range(l, r + 1):
			if prime(i):
				if l <= i + 6 <= r and prime(i + 6):
					l1.append(i)
					l1.append(i + 6)
		if len(l1) == 0:
			l1.append(-1)
			return l1
		else:
			return l1

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


class Solution:

	def sixyPrime(self, L, R):
		c = [True] * (R + 1)
		d = []
		p = 2
		while p * p <= R + 1:
			if c[p] == True:
				for i in range(p * p, R + 1, p):
					c[i] = False
			p = p + 1
		for i in range(L, R - 6 + 1):
			if c[i] == True and c[i + 6] == True:
				d.append(i)
				d.append(i + 6)
		if len(d) == 0:
			return [-1]
		return d


import math

class Solution:

	def sixyPrime(self, L, R):
		primes = [True] * (R + 1)
		p = 2
		while p * p < R + 1:
			if primes[p] == True:
				for i in range(p * p, R + 1, p):
					primes[i] = False
			p += 1
		ans = []
		for i in range(L, R - 5):
			if primes[i] == True and primes[i + 6] == True:
				ans.append(i)
				ans.append(i + 6)
		return ans if len(ans) > 0 else [-1]


class Solution:

	def sixyPrime(self, L, R):
		sieve = [1] * 100005
		sieve[0] = 0
		sieve[1] = 0
		for i in range(2, int(100005 ** 0.5) + 1):
			if sieve[i] == 1:
				for j in range(i * i, 100005, i):
					if sieve[j] == 1:
						sieve[j] = 0
		primes = []
		for i in range(2, int(R ** 0.5) + 1):
			if sieve[i] == 1:
				primes.append(i)
		temp = [1] * (R - L + 1)
		for pr in primes:
			first = L // pr * pr
			if first < L:
				first += pr
			if first in primes:
				first += pr
			for j in range(first, R + 1, pr):
				temp[j - L] = 0
		final = []
		for i in range(R - L - 5):
			if temp[i] == 1:
				if temp[i + 6] == 1:
					final.append(L + i)
					final.append(L + i + 6)
		if len(final) > 0:
			return final
		else:
			return [-1]