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

When working with two numbers, we can check if the difference of their square areas is a prime number. This problem uses the algebraic identity that x² - y² = (x + y)(x - y).

For example, if x = 7 and y = 6, the difference is 49 - 36 = 13, which is prime.

Understanding the Mathematical Approach

The key insight is using the factorization: x² - y² = (x + y)(x - y)

For this product to be prime, one factor must equal 1 (since a prime has only two factors: 1 and itself). Therefore:

  • If (x - y) = 1 and (x + y) is prime, then the difference is prime
  • Otherwise, the difference is either composite or not prime

Implementation

Prime Number Check Function

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

Main Solution

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

# Test the function
x, y = 7, 6
result = solve(x, y)
print(f"x = {x}, y = {y}")
print(f"Difference of squares: {x}² - {y}² = {x*x} - {y*y} = {x*x - y*y}")
print(f"Is the difference prime? {result}")
x = 7, y = 6
Difference of squares: 7² - 6² = 49 - 36 = 13
Is the difference prime? True

Testing Multiple Cases

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):
    return is_prime(x + y) and x - y == 1

# Test cases
test_cases = [(7, 6), (5, 4), (8, 7), (10, 5)]

for x, y in test_cases:
    diff = x*x - y*y
    result = solve(x, y)
    print(f"x={x}, y={y}: {x}²-{y}² = {diff}, Prime: {result}")
x=7, y=6: 7²-6² = 13, Prime: True
x=5, y=4: 5²-4² = 9, Prime: False
x=8, y=7: 8²-7² = 15, Prime: False
x=10, y=5: 10²-5² = 75, Prime: False

How It Works

The algorithm uses the mathematical fact that x² - y² = (x + y)(x - y). For this product to be prime:

  1. One factor must be 1 (since primes have exactly two factors)
  2. The other factor must be the prime number itself
  3. Since x > y for meaningful results, we check if (x - y) = 1
  4. If true, we verify that (x + y) is prime

Conclusion

This solution efficiently checks if the difference of squares is prime by using algebraic factorization. The key insight is that x² - y² = (x + y)(x - y), so we only need to verify that x - y = 1 and x + y is prime.

Updated on: 2026-03-25T14:26:13+05:30

360 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements