Program to find number of solutions for given equations with four parameters in Python

Suppose we have four numbers a, b, c and d. We need to find the number of pairs (x, y) that satisfy the equation: x² + y² = (x*a) + (y*b) where x is in range [1, c] and y is in range [1, d].

For example, if a = 2, b = 3, c = 2, d = 4, then the output will be 1 because one valid pair is (1, 1).

Mathematical Approach

We can rearrange the equation x² + y² = x*a + y*b as a quadratic in y:

y² - b*y + (x² - a*x) = 0

Using the quadratic formula, we get real solutions when the discriminant is non-negative:

discriminant = b² - 4*(x² - a*x) = b² - 4*x*(x-a)

Algorithm Steps

To solve this, we follow these steps ?

  • Initialize count = 0
  • For each x in range [1, c]:
    • Calculate l = x*(x-a)
    • Calculate discriminant det2 = b² - 4*l
    • If det2 == 0 (one solution), check if y = b/2 is valid
    • If det2 > 0 (two solutions), check both y values using quadratic formula
  • Return total count

Implementation

def solve(a, b, c, d):
    count = 0
    
    for x in range(1, c + 1):
        # Calculate x*(x-a) for the quadratic equation
        l = x * (x - a)
        
        # Calculate discriminant: b² - 4*x*(x-a)
        det2 = b * b - 4 * l
        
        # Case 1: One solution (discriminant = 0)
        if det2 == 0 and b % 2 == 0 and 1 <= b // 2 <= d:
            count += 1
            continue
            
        # Case 2: Two solutions (discriminant > 0)
        if det2 > 0:
            det = int(det2 ** 0.5)
            
            # Check if discriminant is a perfect square
            if det * det == det2 and (b + det) % 2 == 0:
                # Check first solution: y = (b + det) / 2
                if 1 <= (b + det) // 2 <= d:
                    count += 1
                    
                # Check second solution: y = (b - det) / 2
                if 1 <= (b - det) // 2 <= d:
                    count += 1
                    
    return count

# Test the function
a, b, c, d = 2, 3, 2, 4
result = solve(a, b, c, d)
print(f"Number of valid pairs (x, y): {result}")
Number of valid pairs (x, y): 1

How It Works

Let's trace through the example with a=2, b=3, c=2, d=4:

def solve_with_trace(a, b, c, d):
    count = 0
    
    for x in range(1, c + 1):
        print(f"\nTesting x = {x}")
        l = x * (x - a)
        det2 = b * b - 4 * l
        
        print(f"  l = x*(x-a) = {x}*({x}-{a}) = {l}")
        print(f"  discriminant = {b}² - 4*{l} = {det2}")
        
        if det2 == 0 and b % 2 == 0 and 1 <= b // 2 <= d:
            print(f"  One solution: y = {b//2}")
            count += 1
            continue
            
        if det2 > 0:
            det = int(det2 ** 0.5)
            if det * det == det2 and (b + det) % 2 == 0:
                y1 = (b + det) // 2
                y2 = (b - det) // 2
                print(f"  Two potential solutions: y = {y1}, y = {y2}")
                
                if 1 <= y1 <= d:
                    print(f"    y = {y1} is valid")
                    count += 1
                if 1 <= y2 <= d:
                    print(f"    y = {y2} is valid")
                    count += 1
                    
    return count

# Trace the example
result = solve_with_trace(2, 3, 2, 4)
print(f"\nTotal valid pairs: {result}")
Testing x = 1
  l = x*(x-a) = 1*(1-2) = -1
  discriminant = 3² - 4*-1 = 13

Testing x = 2
  l = x*(x-a) = 2*(2-2) = 0
  discriminant = 3² - 4*0 = 9
  Two potential solutions: y = 3, y = 0
    y = 3 is valid

Total valid pairs: 1

Conclusion

This algorithm efficiently finds valid pairs by treating the equation as a quadratic in y and checking the discriminant. For each x value, we determine if integer solutions exist within the given bounds.

---
Updated on: 2026-03-26T18:25:30+05:30

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements