karan-and-prime-numbers
Source: hackerearth
Difficulty: EASY

Karan's mathematics professor has given him a lots of homework. Since he is busy preparing for September Easy Challenge, he needs your help.

Given two numbers, find the sum of prime numbers between them, both inclusive. 

Input:

The first line contains the number of test cases T. Each test case contains two space separated integers.

Output:

Print the answer on a new line for each case.

Constraints:

1 ≤ T ≤ 100

1 ≤ a,b < 10^6

SAMPLE INPUT
1
1 5

SAMPLE OUTPUT
10


def generate():
	N = 10**6+1
	mul = set()
	for i in range(2, N):
		if i not in mul:
			yield i
			mul.update(list(range(i*i, N, i)))


primes = [i for i in generate()]
kk = [primes[0]]
for i in primes[1:]:
	kk.append(kk[-1] + i)
prime = set(primes)

T = eval(input())
for i in range(T):
	L, R = list(map(int, input().strip().split()))
	vals = prime.intersection(list(range(L, R+1)))
	if not vals:
		print(0)
	else:
		MIN = min(vals)
		MAX = max(vals)

		MIN_id = primes.index(MIN)
		MAX_id = primes.index(MAX)
		if MIN_id == 0:
			print(kk[MAX_id])
		else:
			print(kk[MAX_id] - kk[MIN_id-1])


def primes_sieve1(limit):
	limitn = limit+1
	primes = dict()
	for i in range(2, limitn): primes[i] = True

	for i in primes:
		factors = list(range(i,limitn, i))
		for f in factors[1:]:
			primes[f] = False
	return [i for i in primes if primes[i]==True]
for i in range(eval(input())):
	s=0
	l=[int(x) for x in input().split()]
	l1=primes_sieve1(l[1])
	for j in range(0,len(l1)):
		if(l1[j]>=l[0]):
			s+=l1[j]
	print(s)


def prime():
	import math
	for _ in range(eval(input())):
		a, b = list(map(int, input().split()))
		sum = 0
		if a == 1 and b != 1:
			a = 2
		elif a == 1 and b == 1:
			print('0')
			continue
		if a == 2:
			sum += 2
			a = 3
		if a % 2 == 0:
			a += 1
			
		for i in range(a, b+1, 2):
			
			flag = 0
			for j in range(2,int(math.sqrt(i)+1)):
				if i%j == 0:
					flag = 1
					break
			
			if flag == 0:
				sum += i
				
			
		print(sum)

if __name__ == "__main__":
	prime()
				


from math import sqrt; from itertools import count, islice

def prime(n):
	if n < 2: return False
	return all(n%i for i in islice(count(2), int(sqrt(n)-1)))


t = int(input())
while t:
	t-=1
	y,z= list(map(int,input().split()))
	summ = 0
	while y<=z:
		if prime(y):
			summ+=y
		y+=1
	print(summ)
		
		


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
import math

def is_prime(n):
	if n <= 3:
		return n >= 2
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, int(n ** 0.5) + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True
	if n % 2 != 0 or (n + 1) % 2 != 0:
		return False
	return True

n = int(input())
i = 0
s = 0
while i < n:
	tc = list(map(int,input().split()))
	j = tc[0]
	while j <= tc[1]:
		if is_prime(j):
			s += j
		j += 1
	i += 1
	print(s)
	s = 0


def isprime(n):
	for i in range(2,int(n**0.5)+1):
		if n%i==0:
			return False
	return True
for p in range(eval(input())):
	c=0
	a,b=list(map(int,input().split()))
	if a==1:
		a=a+1
	if b==1:
		print(0)
		continue
	for i in range(a,b+1):
		if isprime(i):
			#print i
			c=c+i
	print(c)


from math import sqrt

def sieveOfEratosthenes(n):
	primes = list(range(3, n + 1, 2)) # primes above 2 must be odd so start at three and increase by 2
	for base in range(len(primes)):
		if primes[base] is None:
			continue
		if primes[base] > sqrt(n): # stop at sqrt of n
			break

		for i in range(base + (base + 1) * primes[base], len(primes), primes[base]):
			primes[i] = None
	primes.insert(0,2)
	return [_f for _f in primes if _f]

arr=sieveOfEratosthenes(1000000)




for _ in range(eval(input())):
	a,b=list(map(int,input().split()))
	temp=0
	for item in arr:
		if item >=a and item<=b:
			temp=temp+item
		if item>b:
			break
		
	print(temp)


'''
Sum of primes in a range
'''

LIMIT = 10**6
NUMBERS = [True] * LIMIT
NUMBERS[0] = NUMBERS[1] = False

for (i, isprime) in enumerate(NUMBERS):
	if isprime:
		# starting the non-prime marking at the prime's square (i*i) instead of its double.
		# Mark factors non-prime
		for number in range(i*i, LIMIT, i):
			NUMBERS[number] = False

if __name__ == '__main__':
	for _ in range(int(input())):
		a, b = list(map(int, input().split()))
		print(sum(a + num for num, isPrime in enumerate(NUMBERS[a:b + 1]) if isPrime))




def prime():
	import math
	for _ in range(eval(input())):
		a, b = list(map(int, input().split()))
		sum = 0
		if a == 1 and b != 1:
			a = 2
		elif a == 1 and b == 1:
			print('0')
			continue
		if a == 2:
			sum += 2
			a = 3
			
		for i in range(a, b+1):
			
			flag = 0
			for j in range(2,int(math.sqrt(i)+1)):
				if i%j == 0:
					flag = 1
					break
			
			if flag == 0:
				sum += i
				
			
		print(sum)

if __name__ == "__main__":
	prime()
				


import math

def isprime(n):
	if n==1:
		return 0
	for i in range(2,int(math.sqrt(n)+1)):
		if n%i==0:
			return 0
	return 1
	
	
t=eval(input())
while t>0:
	t-=1
	a, b=input().split()
	a, b=int(a), int(b)
	count=0
	for i in range(a,b+1):
		if isprime(i)==1:
			count+=i
			#print i
	print(count)
			


def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2: 
		return True   
	if not n & 1: 
		return False
	for x in range(3, int(n**0.5) + 1, 2):
		if n % x == 0:
			return False

	return True
n=eval(input())
for i in range(n):
	s=input().split(' ')
	a=int(s[0])
	b=int(s[1])
	sum=0
	for j in range(a,b+1):
		if j==1:
			continue
		if(isprime(j)):
			sum+=j
	print(sum)


import math 
def sum_prime(x,y):
	lst = []
	sum1 = 0
	for i in range(x,y+1):
		flag = 0
		for j in range(2,int(math.sqrt(i))+1):
			if i % j == 0:
				flag = 1
				break
		if flag == 0:
			#print(i)
			sum1 += i
	return sum1


if __name__=='__main__':
	t = int(input())
	for i in range(0,t):
		x,y = list(map(int,input().split()))
		if x==1:
			x += 1
		print(sum_prime(x,y))


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

t = int(eval(input()))
while t>0:
	t = t-1
	a = []
	sm = 0
	s,e = list(map(int,input().split(" ")))
	if s>=1 and e>1:
		for i in range(s,e+1):
			if i>2:
				#print "i=",i
				#print prime(i)
				sm = sm+prime(i)
				#print sm
			elif i==2:
				sm=sm+2
	print(sm)


import math

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

t = int(input())
for i in range(0, t):
	s = input()
	s = s.split()
	sum = 0
	if int(s[1]) < 2:
		sum = 0
	if int(s[0]) < 2:
		s[0] = "2"
	for j in range(int(s[0]), int(s[1])+1):
		sum = sum + is_prime(j)
	print(sum)






__author__ = 'josebib'
def is_prime(n):
	if n==1:
		 return False
	if n % 2 == 0 and n > 2:
		return False
	for i in range(3, int(n**0.5) + 1, 2):
		if n % i == 0:
			return False
	return True
def add(f,s):
	sum=0
	for nos in range(f,s+1):
	   
		if is_prime(nos):
			sum+=nos
	return sum

i=eval(input())
for inputs in range(i):
	f,s=input().split(" ")
	print(add(int(f),int(s)))


import math
for _ in range(eval(input())):
	c=0
	a,b=list(map(int,input().split()))
	if a==1:
		a=2
	else:
		a=a
	for num in range(a,b+1):
		if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
		   c+=num
	print(c)




import random

def rabinMiller(num):
	s=num-1
	t=0
	while s%2==0:
		s=int(s/2)
		t=t+1
	for trials in range(5):
		a=random.randrange(2,num-1)
		v=pow(a,s,num)
		if v!=1:
			i=0
			while v!=(num-1):
				if i==t-1:
					return False
				else:
					i=i+1
					v=(v**2)%num
	return True
def isPrime(num):
	if num<2:
		return False
	lowPrimes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
	if num in lowPrimes:
		return True
	for prime in lowPrimes:
		if int(num%prime)==0:
			return False
	return rabinMiller(num)
primeslist={}
case=int(eval(input()))
for i in range(case):
	s=input()
	s=s.split()
	c=0
	for j in range(int(s[0]),int(s[1])+1):
		if j in primeslist:
			if primeslist[j]==True:
				c=c+j
		else:
			if isPrime(j):
				primeslist[j]=True
				c=c+j
			else:
				primeslist[j]=False
			
	print(c)