- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if difference of areas of two squares is prime in Python
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
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
Advertisements