let x and y be the numbers obtained by reversing a and b, if both x and y are prime,then the answer is x+y and if exactly one of x and y is prime, then the answer is a+b.Otherwise ,the answer is a*b.Your task is write a function F(a,b) that takes integers a and b and returns a single digit integer value.Create seperate check_prime(n) and reverse(n) for checking a given number is prime and for reversing number.function must be generalized. Use only loops,if-else or if-elif-else and sting slicing,indexing as well simple inbuilt functions.
primes = [0 for i in range(10000000)]
def sieve(n):
for i in range(2,int(n**1/2 +1)):
if primes[i] ==0:
j = i*i
while j<=n:
primes[j] = 1
j +=i
def check(n):
if primes[n] == 0:
return True
return False
def reverse(a,b):
x,y = int(str(a)[::-1]),int(str(b)[::-1])
return [x,y]
def F(a,b):
x,y = reverse(a,b)
if check(x) and check(y):
return a+b
else:
return a*b
a = int(input())
b = int(input())
sieve(1000000)
print(F(a,b))
Comments
Leave a comment