Pradeep and the his equation program in python

Help

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

Accepted

Pradeep and the his equation program in python
Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.

Time Limit Exceeded

Pradeep and the his equation program in python
Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.

Wrong Answer

Pradeep and the his equation program in python
Your program compiled and ran successfully but the output did not match the expected output.

Runtime Error

Pradeep and the his equation program in python
Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.

Compilation Error

Pradeep and the his equation program in python
Your code was unable to compile. When you see this icon, click on it for more information.

If you are still having problems, see a sample solution here.

Last update on August 19 2022 21:51:44 (UTC/GMT +8 hours)

Python Basic - 1: Exercise-35 with Solution

Write a Python program which solve the equation:
ax+by=c
dx+ey=f
Print the values of x, y where a, b, c, d, e and f are given.

Input:
a,b,c,d,e,f separated by a single space.
(-1,000 ≤ a,b,c,d,e,f ≤ 1,000)
Input the value of a, b, c, d, e, f:
5 8 6 7 9 4
Values of x and y:
-2.000 2.000

Sample Solution:

Python Code:

print("Input the value of a, b, c, d, e, f:")
a, b, c, d, e, f = map(float, input().split())
n = a*e - b*d
print("Values of x and y:")
if n != 0:
    x = (c*e - b*f) / n
    y = (a*f - c*d) / n
    print('{:.3f} {:.3f}'.format(x+0, y+0))

Sample Output:

Input the value of a, b, c, d, e, f:
 5 8 6 7 9 4
Values of x and y:
-2.000 2.000

Flowchart:

Pradeep and the his equation program in python

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to check whether three given lengths (integers) of three sides form a right triangle.
Next: Write a Python program to compute the amount of the debt in n months.

Python: Tips of the Day

Concatenating iterable to a single string:

>>> x = ["python","really", "rocks"]
>>> " ".join(x)
'python really rocks'

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given four integers a, b, c, d ( upto 10^6 ). The task is to Find the number of solutions for x < y, where a <= x <= b and c <= y <= d and x, y integers. 
    Examples
     

    Input: a = 2, b = 3, c = 3, d = 4
    Output: 3
    
    Input: a = 3, b = 5, c = 6, d = 7
    Output: 6

    Approach: Let’s iterate explicitly over all possible values of x. For one such fixed value of x, the problem reduces to how many values of y are there such that c <= y <= d and x = max(c, x + 1) and y <= d. Let’s assume that c <= d, otherwise, there are no valid values of y of course. It follows, that for a fixed x, there are d – max(c, x+1) + 1 valid values of y because the number of integers in a range [R1, R2] is given by R2 – R1 + 1.
    Below is the implementation of the above approach: 
     

    C++

    #include

    using namespace std;

    int NumberOfSolutions(int a, int b, int c, int d)

    {

        int ans = 0;

        for (int i = a; i <= b; i++)

            if (d >= max(c, i + 1))

                ans += d - max(c, i + 1) + 1;

        return ans;

    }

    int main()

    {

        int a = 2, b = 3, c = 3, d = 4;

        cout << NumberOfSolutions(a, b, c, d);

        return 0;

    }

    C

    #include

    int max(int a, int b)

    {

      int max = a;

      if(max < b)

        max = b;

      return max;

    }

    int NumberOfSolutions(int a, int b, int c, int d)

    {

        int ans = 0;

        for (int i = a; i <= b; i++)

            if (d >= max(c, i + 1))

                ans += d - max(c, i + 1) + 1;

        return ans;

    }

    int main()

    {

        int a = 2, b = 3, c = 3, d = 4;

        printf("%d",NumberOfSolutions(a, b, c, d));

        return 0;

    }

    Java

    import java.io.*;

    class GFG

    {

    static int NumberOfSolutions(int a, int b,

                                 int c, int d)

    {

        int ans = 0;

        for (int i = a; i <= b; i++)

            if (d >= Math.max(c, i + 1))

                ans += d - Math.max(c, i + 1) + 1;

        return ans;

    }

    public static void main (String[] args)

    {

        int a = 2, b = 3, c = 3, d = 4;

        System.out.println(NumberOfSolutions(a, b, c, d));

    }

    }

    Python 3

    def NumberOfSolutions(a, b, c, d) :

        ans = 0

        for i in range(a, b + 1) :

            if d >= max(c, i + 1) :

                ans += d - max(c, i + 1) + 1

        return ans

    if __name__ == "__main__" :

        a, b, c, d = 2, 3, 3, 4

        print(NumberOfSolutions(a, b, c, d))

    C#

    using System;

    class GFG

    {

    static int NumberOfSolutions(int a, int b,

                                  int c, int d)

    {

        int ans = 0;

        for (int i = a; i <= b; i++)

            if (d >= Math.Max(c, i + 1))

                ans += d - Math.Max(c, i + 1) + 1;

        return ans;

    }

    public static void Main()

    {

        int a = 2, b = 3, c = 3, d = 4;

        Console.WriteLine(NumberOfSolutions(a, b, c, d));

    }

    }

    PHP

    function NumberOfSolutions($a, $b, $c, $d)

    {

        $ans = 0;

        for ($i = $a; $i <= $b; $i++)

            if ($d >= max($c, $i + 1))

                $ans += $d - max($c, $i + 1) + 1;

        return $ans;

    }

    $a = 2; $b = 3; $c = 3; $d = 4;

    echo NumberOfSolutions($a, $b, $c, $d);

    ?>

    Javascript

    Time Complexity: O(b-a) where a and b are the given integers.
    Auxiliary Space: O(1)