Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Algebra

Is there a way to get php to solve for a variable?

Ex,
1.1x=110
output to screen would be what x is

• Brute force? I'm pretty sure that's how the fancy calculators do it.

PHP Code:
function solveForX(\$equation\$answer\$min null\$max null)
{

\$min = (\$min == null) ? -mt_getrandmax() : \$min;

\$max = (\$max == null) ? mt_getrandmax() : \$max;

\$last['guess'] = null;

while (
true)
{
eval(
'\$attempt = ' str_replace('x', (\$guess mt_rand(\$min\$max)), \$equation) . ';');
if (
{
return(
\$guess);
}
if (
\$last['guess'] != null)
{

if (
\$diff \$last['diff'])
{
if (
\$guess \$last['guess'])
{

\$max \$guess 1;
}
else
{

\$min \$guess 1;
}
}
}

\$last['guess'] = \$guess;

\$last['diff'] = abs(\$answer \$attempt);
}
}

echo
solveForX('x+(x*.1)'110);
Note this is oversimplified and will fall into infinite loops on discontinuous functions and functions with noninteger answers. The latter problem is pretty easily resolvable, the former maybe not so much.

Also, it'd probably be neater if it was refactored to use recursion, another exercise I'll leave to the reader.

• There's some good classes that help handle this.

http://www.phpclasses.org/browse/class/65.html

Check there for some.

• Originally Posted by Kurashu
There's some good classes that help handle this.

http://www.phpclasses.org/browse/class/65.html

Check there for some.
Thanks, I'll go look around there now.

<---edit--->
I found one that I think will do what I need. But how do use the files?

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•