- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find coefficients of linear equations that has only one solution in Python

Suppose we have a value n, we have to find the number of pairs (a, b) [a < b], that exist such that the equation a*x + b*y = n, has at least one solution.

So, if the input is like n = 4, then the output will be 2 because the valid pairs are (1, 2) and (1, 3).

To solve this, we will follow these steps −

- Define a function divisors_gen() . This will take n
- divs := a list of lists of size n+1. And each inner list is holding 1
- divs[0] := a list with only one element 0
- for i in range 2 to n, do
- for j in range 1 to floor of (n / i) + 1, do
- insert i at the end of list at index [i * j]

- for j in range 1 to floor of (n / i) + 1, do
- return divs but reverse all internal lists
- From the main method, do the following −
- result := 0
- d_cache := divisors_gen(n+1)
- for a in range 1 to n - 1, do
- i := 1
- s := a new set
- while a*i < n, do
- b := n - a*i
- for each d in d_cache[b], do
- if d > a, then
- if d not in s, then
- result := result + 1

- if d not in s, then
- otherwise,
- come out from the loop

- insert d into the set s

- if d > a, then
- i := i + 1

- return result

## Example

Let us see the following implementation to get better understanding −

def divisors_gen(n): divs = [[1] for x in range(0, n + 1)] divs[0] = [0] for i in range(2, n + 1): for j in range(1, n // i + 1): divs[i * j].append(i) return [i[::-1] for i in divs] def solve(n): result = 0 d_cache = divisors_gen(n+1) for a in range(1, n): i = 1 s = set([]) while a*i < n: b = n - a*i for d in d_cache[b]: if d > a: if d not in s: result += 1 else: break s.add(d) i += 1 return result n = 4 print(solve(n))

## Input

4

## Output

2

- Related Questions & Answers
- C program to find the solution of linear equation
- Find the Number of Solution to Modular Equations using C++
- Solution of Difference Equations Using Z-Transform
- C++ Program to Represent Linear Equations in Matrix Form
- Solve a linear matrix equation or system of linear scalar equations in Python
- How to find the standardized coefficients of a linear regression model in R?
- How to solve the simultaneous linear equations in R?
- Program to find total number of strings, that contains one unique characters in Python
- Program to find sum of digits that are inside one alphanumeric string in Python
- Program to find tree level that has minimum sum in C++
- Program to find number of solutions for given equations with four parameters in Python
- Program to find number m such that it has n number of 0s at end in Python
- Which linear function of SciPy is used to solve triangular matrix equations?
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- Program to find root of an equations using secant method in C++

Advertisements