Given two integers M and N, generate all primes between M and N including M and N.
Example 1:
Input:
M=1,N=10
Output:
2 3 5 7
Explanation:
The prime numbers between 1 and 10
are 2,3,5 and 7.
Example 2:
Input:
M=2, N=5
Output:
2,3,5
Explanation:
The prime numbers between 2 and 5 are 
2,3 and 5.
Your Task:
You don't need to read input or print anything. Your task is to complete the function primeRange() which takes 2 integer inputs M and N and returns a list of all primes between M and N including N and M.
Expected Time Complexity:O(N*sqrt(N))
Expected Auxillary Space:O(1)
Constraints:
1<=M<=N<=10^{6}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Number theory

Starter code: #User function Template for python3



class Solution:        

    def primeRange(self,M,N):

        #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 primeRange(self, M, N):
		l = []
		for i in range(M, N + 1, 1):
			if self.isprime(i):
				l.append(i)
		return l


class Solution:

	def prime(self, n):
		k = math.sqrt(n)
		k = int(k) + 1
		if n == 2 or n == 3 or n == 5 or (n == 7):
			return 1
		if n <= 1:
			return 0
		if n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or (n % 7 == 0):
			return 0
		for i in range(2, k + 1):
			if n % i == 0:
				return 0
		return 1

	def primeRange(self, M, N):
		a = []
		for i in range(M, N + 1):
			if self.prime(i):
				a.append(i)
		return a


class Solution:

	def primeRange(self, M, N):

		def Isprime(N):
			if N <= 1:
				return False
			for i in range(2, int(N ** 0.5) + 1):
				if N % i == 0:
					return False
			return True
		x = []
		for i in range(M, N + 1):
			if Isprime(i):
				x.append(i)
		return x


class Solution:

	def primeRange(self, m, n):
		ans = []
		if m == 1:
			m += 1
		import math
		while m <= n:
			for i in range(m, n + 1):
				c = 0
				for j in range(2, int(math.sqrt(i)) + 1):
					if i % j == 0:
						c += 1
						break
				if c == 0:
					ans.append(i)
			return ans


import math

def is_prime(N):
	if N < 2:
		return False
	s = int(math.sqrt(N))
	for i in range(2, s + 1):
		if N % i == 0:
			return False
	return True

class Solution:

	def primeRange(self, M, N):
		primes = []
		for i in range(M, N + 1):
			if is_prime(i) == True:
				primes.append(i)
		return primes


class Solution:

	def primeRange(self, M, N):
		if M == 0:
			M = 1
		b = [1 for i in range(N + 1)]
		for i in range(2, N + 1):
			if b[i] == 1:
				j = 2 * i
				while j <= N:
					b[j] += 1
					j += i
		ans = []
		for i in range(M, N + 1):
			if b[i] == 1 and i != 1:
				ans.append(i)
		return ans


class Solution:

	def primeRange(self, M, N):
		pr = [True] * (N + 1)
		pr[0] = pr[1] = False
		l = []
		i = 2
		while i * i <= N:
			if pr[i] == True:
				for j in range(i * i, N + 1, i):
					pr[j] = False
			i += 1
		for i in range(M, N + 1):
			if pr[i] == True:
				l.append(i)
		return l


from math import sqrt

class Solution:

	def primeRange(self, M, N):
		s = []
		for i in range(M, N + 1):
			flag = 0
			if i > 1:
				for j in range(2, int(sqrt(i)) + 1):
					if i % j == 0:
						flag = 1
						break
				if flag == 0:
					s.append(i)
		return s


def p(b):
	if b == 1:
		return -1
	else:
		a = int(b ** (1 / 2))
		for j in range(2, a + 1):
			if b % j == 0:
				return -1
	return b

class Solution:

	def primeRange(self, M, N):
		c = []
		for k in range(M, N + 1):
			if p(k) != -1:
				c.extend([k])
		return c


class Solution:

	def primeRange(self, M, N):
		primes = []
		is_prime = [True] * (N + 1)
		p = 2
		while p * p <= N:
			if is_prime[p]:
				for i in range(p * p, N + 1, p):
					is_prime[i] = False
			p += 1
		for i in range(max(2, M), N + 1):
			if is_prime[i]:
				primes.append(i)
		return primes


from math import *

class Solution:

	def primeRange(self, s, e):

		def isprime(n):
			if n > 1:
				for i in range(2, int(sqrt(n)) + 1):
					if n % i == 0:
						return 0
				else:
					return 'surya'
			else:
				return 0
		l = []
		for i in range(s, e + 1):
			if isprime(i) == 'surya':
				l.append(i)
		return l


class Solution:

	def primeRange(self, M, N):
		l = []
		for i in range(M, N + 1):
			if i > 1:
				for j in range(2, int(i ** 0.5) + 1):
					if i % j == 0:
						break
				else:
					l.append(i)
		return l


class Solution:

	def primeRange(self, M, N):
		primes = [True for i in range(N + 1)]
		for i in range(2, int(math.sqrt(N)) + 1):
			if primes[i]:
				for j in range(i * i, N + 1, i):
					primes[j] = False
		res = []
		for i in range(M, N + 1):
			if primes[i] and i != 1 == True:
				res.append(i)
		return res


from math import sqrt

class Solution:

	def primeRange(self, M, N):
		arr = []
		for j in range(M, N + 1):
			flag = True
			if j == 1:
				continue
			for i in range(2, int(sqrt(j)) + 1):
				if j % i == 0:
					flag = False
					break
			if flag:
				arr.append(j)
		return arr


class Solution:

	def primeRange(self, m, n):

		def isprime(n):
			if n <= 1:
				return False
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		pr = []
		for i in range(m, n + 1):
			if isprime(i):
				pr.append(i)
		return pr


class Solution:

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

	def primeRange(self, M, N):
		primes = []
		for num in range(M, N + 1):
			if self.is_prime(num):
				primes.append(num)
		return primes


from math import sqrt

class Solution:

	def primeRange(self, M, N):
		a = [True] * (N + 1)
		(a[0], a[1], l) = (False, False, [])
		for i in range(2, int(sqrt(N)) + 1):
			if a[i]:
				for z in range(i + i, N + 1, i):
					a[z] = False
		for p in range(M, len(a)):
			if a[p]:
				l.append(p)
		return l


class Solution:

	def primeRange(self, M, N):

		def prime(n):
			if n == 1 or n == 0:
				return 0
			k = int(n ** 0.5)
			for i in range(2, k + 1):
				if n % i == 0:
					return 0
			return 1
		l = []
		for i in range(M, N + 1):
			if prime(i):
				l.append(i)
		return l


class Solution:

	def primeRange(self, left, right):
		sieve = [True for _ in range(right + 1)]
		sieve[0] = False
		sieve[1] = False
		i = 2
		while i * i <= right:
			for j in range(i * i, right + 1, i):
				sieve[j] = False
			i += 1
		res = []
		for i in range(left, right + 1):
			if sieve[i] == True:
				res.append(i)
		return res


import math

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

class Solution:

	def primeRange(self, M, N):
		l = []
		for i in range(M, N + 1):
			if isprime(i) == 1:
				l.append(i)
		return l


class Solution:

	def primeRange(self, m, n):
		l = []
		for i in range(n + 1):
			l.append(True)
		p = 2
		while p * p <= n + 1:
			if l[p] == True:
				for i in range(p * p, n + 1, p):
					l[i] = False
			p += 1
		f = []
		for i in range(m, n + 1):
			if l[i] and i != 1:
				f.append(i)
		return f


import math

class Solution:

	def primeRange(self, M, N):
		s = [True] * (N + 1)
		s[0] = False
		s[1] = False
		a = int(math.sqrt(N))
		for i in range(2, a + 1):
			for j in range(i + i, N + 1, i):
				s[j] = False
		l = []
		for i in range(M, N + 1):
			if s[i] == True:
				l.append(i)
		return l


import math

class Solution:

	def primeRange(self, M, N):
		l = []
		for i in range(M, N + 1):
			c = 0
			if i == 1:
				continue
			x = int(math.sqrt(i))
			for j in range(2, x + 1):
				if i % j == 0:
					c = 1
					break
			if c == 0:
				l.append(i)
		return l


import math

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

class Solution:

	def primeRange(self, M, N):
		d = []
		for i in range(M, N + 1):
			if prime(i):
				d.append(i)
		return d


class Solution:

	def primeRange(self, M, N):
		a = []
		for s in range(M, N + 1):
			if s > 1:
				for i in range(2, int(s ** 0.5) + 1):
					if s % i == 0:
						break
				else:
					a.append(s)
		return a


import math as m

class Solution:

	def primeRange(self, M, N):
		arr = [True] * (N + 1)
		arr[0] = arr[1] = False
		s = round(m.sqrt(N))
		for i in range(2, s + 1):
			if arr[i]:
				for j in range(i * i, len(arr), i):
					arr[j] = False
		a = []
		for i in range(M, N + 1):
			if arr[i]:
				a += [i]
		return a


import math as m

class Solution:

	def primeRange(self, M, N):
		a = []
		for i in range(M, N + 1):
			if fun(i) == True:
				a += [i]
		return a

def fun(n):
	if n == 1 or n == 0:
		return False
	for i in range(2, round(m.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True


class Solution:
	N = 10 ** 6
	primes = [1] * (N + 1)
	primes[0] = primes[1] = 0
	p = 2
	while p * p <= N:
		for i in range(p * p, N + 1, p):
			primes[i] = 0
		p += 1

	def primeRange(self, M, N):
		return [i for i in range(M, N + 1) if self.primes[i]]


class Solution:

	def primeRange(self, M, N):

		def isPrime(N):
			import math
			if N < 2:
				return 0
			x = int(math.sqrt(N))
			for i in range(2, x + 1):
				if N % i == 0:
					return 0
			return 1
		primes = []
		for i in range(M, N + 1):
			if isPrime(i) == True:
				primes.append(i)
		return primes


import math

class Solution:

	def primeRange(self, p, q):
		l = []
		for j in range(p, q + 1):
			n = j
			for r in range(2, int(math.sqrt(n) + 1)):
				if n % r == 0:
					break
			else:
				if n != 1:
					l.append(n)
		return l


class Solution:

	def primeRange(self, a, b):
		ans = []

		def prime(n):
			if n < 2:
				return 0
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return 0
			return 1
		c = 0
		for i in range(a, b + 1):
			if prime(i):
				ans.append(i)
		return ans


from math import sqrt

class Solution:

	def primeRange(self, M, N):

		def prime(a):
			if a < 2:
				return 0
			for i in range(2, int(sqrt(a)) + 1):
				if a % i == 0:
					return 0
			return 1
		l = []
		for j in range(M, N + 1):
			if prime(j) == 1:
				l.append(j)
		return l


import math

def prime(s):
	k = int(math.sqrt(s))
	if s < 2:
		return False
	for i in range(2, k + 1):
		if s % i == 0:
			return False
	else:
		return True

class Solution:

	def primeRange(self, M, N):
		lst = []
		for i in range(M, N + 1):
			if prime(i) == True:
				lst.append(i)
		return lst