Jump to content

PHP algorithm for solving a simple linear equation.


ftpkid

Recommended Posts

Hello, I'm a beginner and i just wrote an extremely simple program for solving a linear equation. It works, but i was wondering if you guys can take a look. Here it is:

<?php$a = 5;$b = 10;$r = -($b/$a);if($a == 0 && $b == 0) {echo 'There are infinitely many solutions';}elseif($a>0 && $b>0) {echo $r;}elseif($a>0 && $b == 0) {echo 'X is equal to zero';}else {echo 'There are no solutions';}?>

It's really, really basic and some parts aren't really correct(i guess). Please, tell me what you think. It isn't really complete, because the variable "a", might be a negative number. Also the code isn't exactly the most beautiful one... I didn't make an HTML form, so that the user can write the constants, on purpose.

Link to comment
Share on other sites

Your code doesn't seem to be accurate for negative values of A and B. It seems you did note that in your post. Is this the equation you're trying to solve? x = -(b/a)

Link to comment
Share on other sites

There's a simpler way to do it: As long as $a is not 0, perform the division. If $a is 0 then check the value of $b. If $b is 0 then there are infinite possile answers, otherwise the answer is infinity.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...