Check if difference of areas of two squares is prime in Python

PythonServer Side ProgrammingProgramming

Suppose we have two numbers x and y. We have to check whether difference of their areas is prime or not.

So, if the input is like x = 7, y = 6, then the output will be True as the difference of their square is 49 - 36 = 13 which is prime.

To solve this, we will follow these steps −

  • if (x + y) is prime number and (x - y) is 1, then
    • return True
  • otherwise,
    • return False

Let us see the following implementation to get better understanding −

Example

 Live Demo

def is_prime(num) :
   if num <= 1 :
      return False
   if num <= 3 :
      return True
   if num % 2 == 0 or num % 3 == 0 :
      return False
   i = 5
   while i * i <= num:
      if num % i == 0 or num % (i + 2) == 0:
         return False
      i = i + 6
   return True
def solve(x, y):
   if is_prime(x + y) and x - y == 1:
      return True
   else:
      return False
x, y = 7, 6
print(solve(x, y))

Input

7,6

Output

True
raja
Published on 30-Dec-2020 18:20:47
Advertisements