Given is a range of integers [a , b]  . You have to find the sum of all the numbers in the range  [a , b] that are prime as well as palindrome.
 
Example 1:
Input:
a = 1, b = 10
Output:
17
Explanation:
prime numbers between 1 and 10 are 2, 3,
5, and 7. All of these are Palindromes.
So, sum = 17.
Example 2:
Input:
a = 11, b = 13
Output:
11
Explanation:
prime numbers between 11 and 13 are 11
and 13. Omly 11 is Palindrome among these.
So, sum = 11. 
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function getPPS() which takes 2 Integers a and b as input and returns an integer as the answer.
 
Expected Time Complexity: O(loglog(B))
Expected Auxiliary Space: O(B)
 
Constraints:
1 <= A <= B <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

String algorithms

Starter code: #User function Template for python3

class Solution:
    def getPPS(self, a, b):
        # code here 

Solutions:
import math

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

class Solution:

	def getPPS(self, a, b):
		c = 0
		for i in range(a, b + 1):
			if fun(i):
				if str(i) == str(i)[::-1]:
					c += i
		return c


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

def pal(n):
	s = str(n)
	a = s[::-1]
	if s == a:
		return True
	else:
		return False

class Solution:

	def getPPS(self, a, b):
		c = 0
		for i in range(a, b + 1):
			if prime(i) == True and pal(i) == True:
				c = c + i
		return c


class Solution:

	def getPPS(self, a, b):

		def isprime(l):
			if l <= 1:
				return False
			elif l == 2:
				return True
			for i in range(2, int(l / 2) + 1):
				if l % i == 0:
					return False
			else:
				return True
		sum = 0
		for j in range(a, b + 1):
			if j == int(str(j)[::-1]) and isprime(j) == True:
				sum += j
		return sum


class Solution:

	def getPPS(self, a, b):

		def prime(n):
			c1 = 0
			for i in range(2, n // 2 + 1):
				if n % i == 0:
					return 0
			if n != 1:
				return 1
		sum = 0
		for i in range(a, b + 1):
			if str(i) == str(i)[::-1]:
				if prime(i) == 1:
					sum += i
		return sum


class Solution:

	def getPPS(self, a, b):

		def p(x):
			if x == 0 or x == 1:
				return False
			for i in range(2, int(x ** 0.5) + 1):
				if x % i == 0:
					return False
					break
			return True
		s = 0
		for i in range(a, b + 1):
			if p(i) == True and str(i) == str(i)[::-1]:
				s += i
		return s


class Solution:

	def getPPS(self, a, b):
		if a == 1:
			a = 2
		l1 = []
		main = 0
		for i in range(a, b + 1):
			count = 0
			for j in range(2, int(i ** 0.5) + 1):
				if i % j == 0:
					count = 1
					break
			if count == 0:
				l1.append(i)
		for i in l1:
			a = int(str(i)[::-1])
			if a == i:
				main = main + i
		return main


class Solution:

	def getPPS(self, a, b):
		x = 0
		for i in range(a, b + 1):
			if i == 1:
				continue
			if i == int(str(i)[::-1]):
				x1 = len([j for j in range(2, int(i ** 0.5) + 1) if i % j == 0])
				if x1 == 0:
					x += i
		return x


class Solution:

	def getPPS(self, a, b):

		def isprime(n):
			if n == 1:
				return False
			if n == 2:
				return True
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		ans = 0
		for i in range(a, b + 1):
			b = str(i)
			if b == b[::-1] and isprime(i):
				ans += i
		return ans


class Solution:

	def getPPS(self, a, b):
		s = 0
		l1 = []
		for i in range(a, b + 1):
			if i > 1:
				for j in range(2, int(i ** (1 / 2)) + 1):
					if i % j == 0:
						break
				else:
					l1.append(i)
		for a in l1:
			b = str(a)
			c = b[::-1]
			if b == c:
				s = s + a
		return s


class Solution:

	def getPPS(self, a, b):
		import math

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

		def is_palindrome(n: int) -> bool:
			return n == int(str(n)[::-1])
		result = 0
		for i in range(a, b + 1):
			if is_prime(i) and is_palindrome(i):
				result += i
		return result


class Solution:

	def getPPS(self, a, b):

		def sieve(a, n):
			primes = [True] * (n + 1)
			primes[0] = False
			primes[1] = False
			for i in range(2, n + 1):
				if primes[i]:
					for j in range(i * i, n + 1, i):
						primes[j] = False
			lst = [i for i in range(a, n + 1) if primes[i]]
			return lst
		p = sieve(a, b)
		lst = [i for i in p if str(i) == str(i)[::-1]]
		return sum(lst)


import math

class Solution:

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

	def getPPS(self, a, b):
		summ = 0
		for i in range(a, b + 1):
			if self.isPrime(i):
				x = str(i)
				if x == x[::-1]:
					summ += i
		return summ


class Solution:

	def getPPS(self, a, b):
		t = []
		if a == 1:
			a = a + 1
		for i in range(a, b + 1):
			k = []
			r = str(i)
			m = r[::-1]
			if m != r:
				continue
			for j in range(2, int(i ** (1 / 2)) + 1):
				if i % j == 0:
					k.append(i)
					break
			if len(k) == 0:
				t.append(i)
		return sum(t)


class Solution:

	def getPPS(self, a, b):
		prime = [0] * (b + 1)
		for i in range(2, b + 1):
			prime[i] = i
		for i in range(2, int(b ** 0.5) + 1):
			if prime[i] == i:
				for j in range(i * i, b + 1, i):
					prime[j] = 0
		sumval = 0
		for i in range(a, b + 1):
			if prime[i] == i:
				x = str(i)
				if x == x[::-1]:
					sumval += i
		return sumval


import math

class Solution:

	def checkPrime(self, n):
		if n == 1:
			return False
		elif n == 2:
			return True
		for itr in range(2, math.ceil(math.sqrt(n)) + 1):
			if n % itr == 0:
				return False
		return True

	def getPPS(self, a, b):
		total = 0
		for itr in range(a, b + 1):
			if str(itr) == str(itr)[::-1] and self.checkPrime(itr):
				total += itr
		return total
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		ob = Solution()
		print(ob.getPPS(a, b))


import math

class Solution:

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

	def getPPS(self, a, b):
		summ = 0
		for i in range(a, b + 1):
			if self.isPrime(i):
				x = str(i)
				if x == x[::-1]:
					summ += i
		return summ
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		ob = Solution()
		print(ob.getPPS(a, b))


class Solution:

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

	def getPPS(self, a, b):
		c = 0
		if a == 1:
			a += 1
		for i in range(a, b + 1):
			if self.ispr(i):
				d = str(i)
				if d == d[::-1]:
					c += i
		return c
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		ob = Solution()
		print(ob.getPPS(a, b))


import math

class Solution:

	def getPPS(self, a, b):
		l = 100001
		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, l, i):
					siv[j] = 1

		def pal(n):
			t = n
			digit = 0
			rev = 0
			while n != 0:
				digit = n % 10
				rev = rev * 10 + digit
				n //= 10
			if rev == t:
				return True
			return False
		s = 0
		for i in range(a, b + 1):
			if siv[i] == 0 and pal(i) == True:
				s += i
		return s


import math

class Solution:

	def getPPS(self, a, b):
		sum1 = 0
		for i in range(a, b + 1):
			if isprime(i) == 1:
				if ispalindrome(i) == True:
					sum1 += i
		return sum1

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

def ispalindrome(n):
	n1 = str(n)
	if n1 == n1[::-1]:
		return True
	else:
		return False


from math import sqrt

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

def palin(n):
	if n < 10:
		return 1
	else:
		t = int(str(n)[::-1])
		if t == n:
			return 1

class Solution:

	def getPPS(self, a, b):
		sum = 0
		for i in range(a, b + 1):
			if isprime(i) and palin(i):
				sum += i
		return sum


from math import *

def isPrime(n):
	if n == 1:
		return 0
	if all([n % e > 0 for e in range(2, int(sqrt(n)) + 1)]):
		return 1
	return 0

def isPalindrome(n):
	s1 = str(n)
	s2 = s1[::-1]
	if s1 == s2:
		return 1
	return 0

class Solution:

	def getPPS(self, a, b):
		s = 0
		for e in range(a, b + 1):
			if isPalindrome(e) and isPrime(e):
				s += e
		return s


class Solution:

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

	def getPPS(self, a, b):
		s = 0
		for i in range(a, b + 1):
			if i == int(str(i)[::-1]) and self.isPrime(i):
				s += i
		return s


from math import sqrt

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

def ispalin(N):
	if N < 10:
		return True
	if str(N) == str(N)[::-1]:
		return True
	return False

class Solution:

	def getPPS(self, a, b):
		sum = 0
		for i in range(a, b + 1):
			if isprime(i) == True and ispalin(i) == True:
				sum += i
		return sum