Python Forum
No Idea Where To Start...
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No Idea Where To Start...
#1
Python3

I have no idea where to start for this problem....

Write a program that solves a linear equation ax = b in integers. Given two integers a and b (a may be zero), print a single integer root if it exists and print "no solution" or "many solutions" otherwise.

Example Input #1:
1
-2


Example Output #1:
-2

Example Input #2:
2
-1


Example Output #2:
no solution
Reply
#2
We generally don't write code for people here, we prefer to help people fix code they've written. You can solve this with division, and if/else statement, and the int() function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Some times the easiest way is: how would you solve this with pencil and paper? In other words, if you know a and b, what would the formula be to solve for x?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
You can simply use the basic equation and manipulate it to get x equal to the other two variables. This would result in simply setting x=b/a. This assumes you have already gotten the input from your user. This will be done by a=int(input()) and b=.... If a happens to equal 0 from the input, you will have to set up an if statement to prevent it from reaching the division sign which will break your program. You will have infinite solutions when a and b are equivalent to eachother.
Reply
#5
Please do not post actual solutions to questions in this (Homework) forum, as it may prove detrimental to the student. Instead provide guidance and suggestion to help the student arrive at their own solution.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020