How to solve a math equation in a programming language?

18,219

Solution 1

<?php

// ((x * n) + y)/(n + 1) = z)
// => n=(y-z)/(z-x)
function eq ($x=0,$y=0,$z=0)
{
    if ($z!=$x)
    {
        $n=($y-$z)/($z-$x);
    } else
    {
        $n='NAN';
    }
    return $n;
}

?>

(My algebra is old and flakey but I think this is right)

Solution 2

You can use http://pear.php.net/package/PHP_ParserGenerator/redirected to parse the math expressions into a syntax tree, then do the maths.

((n * 2) + 10) / (n + 1) = 3 would look like:

enter image description here

The idea is to bring on the right subtree (here ...) all the numbers, and on the left all the unknownws, just as you'd do on paper.

In the end you'll have:

  +
 / \
n  -7

which is 0. And there you have your solution, for any math expression (with one unknown variable).

I'll leave the algorithm to you.

Solution 3

how about using brute-force??!?! might be slow and not exact:

$step = 0.00001;
$err = 0.1; //error margin
$start = 0;
$response = 3;

for($i = $start;$i <= 3;$i += $step){
   if((($i * 2) + 10) / ($i + 1) >= $response - $err){
       echo "the answer is $i";
   }
}

You could improove this answer.. on every loop you could calculate the distance between the current answer and the desired answer, and adjust the parameters acording to that..

This reminds me my old A.I. class =)

Good Luck

Share:
18,219
xidew
Author by

xidew

Updated on June 30, 2022

Comments

  • xidew
    xidew almost 2 years

    I need help to solve this formula ((n * 2) + 10) / (n + 1) = 3, preferably in PHP. (The numbers 2, 10 and 3 should be variables that can be changed.)

    I'm able to solve this equation on paper quite easily. However, when I try to implement this in PHP, I'm not sure where to start. I've done several Google queries and searches on here and nothing seems to help. I'm missing the proper approach to deal with this problem.

    Any tips and pointers would be great, and if you provide the exact code, please explain how you got to this result.

  • xidew
    xidew over 12 years
    Your function works! Would you mind explaning what was your thought process when you were writing/thinking about it?
  • xidew
    xidew over 12 years
    That's a solution, but I'm looking for the proper way to solve it. Thanks for your answer though!
  • xidew
    xidew over 12 years
    Very interesting, I will have a look :)
  • Benjamin Crouzier
    Benjamin Crouzier over 12 years
    Your function is mathematically incorect. You're missing the case $y == $x && $z == $x. In such a case, the esemble R - { 1 } is solution of the equation.
  • Benjamin Crouzier
    Benjamin Crouzier over 12 years
    Also, if i call eq(0, 0, 1);, your function yields -1, yet ((x * n) + y)/(n + 1) = z) gives ((0 * -1) + 0)/(-1 + 1) = 1) which is impossible
  • DaveyBoy
    DaveyBoy over 12 years
    My get out clause is that my algebra is old and flakey and not something I use everyday. The main thing was to check for division by 0 (hence the check for Z<>X). My thinking behind the solution was to get the equation into the form of x*n=c (even if x is a fraction). Once it's in that form, the solution was a simple re-write of the equation into PHP
  • DaveyBoy
    DaveyBoy over 12 years
    @pinouchon - obviously, more checks can be added for special cases. However, I'm a programmer and not a mathematician so my caveat is still valid - my algebra is old and flakey but I think this is right
  • Benjamin Crouzier
    Benjamin Crouzier over 12 years
    @DaveWilsonLAMP check my answer where all special cases are handled. The drawbacks of not handling special cases is that in some cases, you answer is just wrong, eg eq(0,0,0), eq(1,1,1), eq(0,0,1), eq(1,1,2)...
  • DaveyBoy
    DaveyBoy over 12 years
    @pinchoun - As I said I am not a mathematician, I am a programmer. As such, I drew on my algebra classes from 25 years ago to rearrange the original equation into something which will allow variables to be plugged in and an answer produced. The main thing that stood out was the "divide by 0" problem so I checked for that. Special cases did not occur to me as I did not run a whole load of numbers through the function to check for special cases - I don't have time to do that. My original answer included "I think this is right" so I was open to correction
  • xidew
    xidew over 12 years
    I decided to give DaveWilsonLAMP the correct answer because he was the first who wrote the function even though it wasn't as complete as pinouchon, still was correct. @pinouchon have also a correct answer however, now I wish I could accept both of them...
  • xidew
    xidew over 12 years
    @Flavius I actually voted up your answer. I didn't pick it because it was hard to understand at first but I understand what you were going for. What I learned from Dave and pinouchon answers is that the answer I was looking for was a simplified version of my equation. I learned a lot from all the answers here and I'm really grateful, unfortunately I can only pick one good answer... :-|
  • Carlos
    Carlos almost 2 years
    And how do you move all the numbers to the right side? Could you give us an example please? Thanks!