
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find number of solutions for given equations with four parameters in Python
Suppose we have four numbers a, b, c and d and We have to find number of pairs (x, y) can be found such that that follows the equation: x^2 + y^2 = (x*a) + (y*b) where x in range [1, c] and y in range [1, d]
So, if the input is like a = 2 b = 3 c = 2 d = 4, then the output will be 1 because one pair is (1, 1).
To solve this, we will follow these steps −
- ans := 0
- for x in range 1 to c, do
- l := x*(x-a)
- det2 := b*b - 4*l
- if det2 is same as 0 and b is even and 1 <= floor of (b/2) <= d, then
- ans := ans + 1
- go for next iteration
- if det2 > 0, then
- det := integer part of the square root of det2
- if det^2 is same as det2 and (b+det) is even, then
- if 1 <= floor of (b+det)/2 <= d, then
- ans := ans + 1
- if 1 <= floor of (b-det)/2 <= d, then
- ans := ans + 1
- if 1 <= floor of (b+det)/2 <= d, then
- return ans
Example
Let us see the following implementation to get better understanding −
def solve(a, b, c, d): ans = 0 for x in range(1,c+1): l = x*(x-a) det2 = b*b - 4*l if det2 == 0 and b%2 == 0 and 1 <= b//2 <= d: ans += 1 continue if det2 > 0: det = int(round(det2**0.5)) if det*det == det2 and (b+det) % 2 == 0: if 1 <= (b+det)//2 <= d: ans += 1 if 1 <= (b-det)//2 <= d: ans += 1 return ans a = 2 b = 3 c = 2 d = 4 print(solve(a, b, c, d))
Input
2, 3, 2, 4
Output
1
- Related Articles
- C/C++ Program for Number of solutions to Modular Equations?
- Program for Number of solutions to Modular Equations in C/C++?
- C/C++ Program for Number of solutions to the Modular Equations?
- Find the number of solutions to the given equation in C++
- Program to find number of tasks can be finished with given conditions in Python
- Program to find number of solutions in Quadratic Equation in C++
- Python Program for Number of elements with odd factors in the given range
- Find four solutions of $x + y=10$
- Program to find number of bit 1 in the given number in Python
- Program to find number of coins needed to make the changes with given set of coins in Python
- Program to find last digit of the given sequence for given n in Python
- Program to find number of given operations required to reach Target in Python
- Program to find the sum of all digits of given number in Python
- Program to convert gray code for a given number in python
- Python program to find number of days between two given dates

Advertisements