Jump to content

php and algebra


Go to solution Solved by Barand,

Recommended Posts

is it possable to code this in php?

its for a game and i want to be ale to work out how many battles i have to play to increase my winrate by 1%

ive been old the equation below will do this but i have no idea how to code it.

 

 

 

p and c comes from a html form

 

 

p = Number of Battles played

c = Win %

w = Battles Won

N = number of games need for 1% increase

 

 

(w + N) = (C+1)/100 (p + N)

 

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/272988-php-and-algebra/
Share on other sites

this what i got so far. a friend who understands algebra more than me tryed to explain bit by bit and i tryed to code it as i went but i got lost quickly lol

only thing missing is the form

 

$bat = $_GET["battle"];
$win = $_GET["winr"];

echo "Battles :".$bat."</br>";
echo "Win Rate :".$win."%</br>";

$step1 = $bat * ($win/100);
echo "Battle Won :".$step1."</br>";
$increase = 1;
$step2 = $step1 * (1 + ($increase/10));

echo "</br>";
echo "</br>";

$step3 = $step2 - $win;
echo $step3;

Link to comment
https://forums.phpfreaks.com/topic/272988-php-and-algebra/#findComment-1404937
Share on other sites

To solve an equation in a programming language, you'll want to move all the operations to one side. Which makes the entire equation equals 0. In this case, the equation would look like this then:

0 = (C+1) / 100 * (p+N) - (w+N)

 

Since you know the number of battles (p) and the win percentage (C), you also know the number of battles won (w). Which leaves only the number of battles needed (N) as the remaining unknown factor. Personally, I'd use number of battles and number of battles won, and then calculate the percentage from that. Seeing as the percentage is a calculated value anyway, which is derived from the two integers p and w

Anyway, that's a digression. Back to solving the equation...

 

We continue by expanding the parentheses, which is done by multiplying (or dividing, as the case might be) the value on the outside with the values on the inside.

That leaves us with the following:

0 = (C*p + C*N + 1*p + 1*N) / 100 - (w+N)

 

Then we resolve the division, using the same rule, and expands the final pair of parentheses by multiplying their values with -1:

0 = C*p/100 + C*N/100 + 1*p/100 + 1*N/100 - w - N

 

Having the /100 bit in there makes things look a bit more complicated than necessary, so let's just multiply everything by 100 to get rid of it:

0 = C*p + C*N + p + N - 100*w - 100*N

 

What we need to do now, is to move all of the N-values to one of the equation:

100*N - C*N - N = C*p + p - 100*w

 

Collapsing the 100N - N gives us 99N, and by extracting the N-value (by adding a pair of parentheses) we make the equation look like this:

N*(99 - C) = C*p + p - 100*w

 

Once that is done, we can finally move the rest of the non-N-values to the right hand side again, by dividing with the parentheses.

N = (C*p + p - 100*w) / (99 - C)

 

Now that we got this, we can substitute the letters with the known PHP variables. Let's say that the following is true:

// Known
p = $Battles
w = $Won
C = $Percent = round ($Won / $Battles * 100, 0)

// Unknown
N = $Needed

 

That gives us a PHP equation which looks like this:

// Retrieve the number of battles from the URL.
$Battles = (int) $_GET['battles'];
$Won = (int) $_GET['won'];

// Calculate the percentage of battles won.
$Percent = round ($Won/$Battles*100, 0);

// Calculate the number of battles needed to increase win percentage by 1%.
$Needed = ($Percent*$Battles + $Battles - 100*$Won) / (99 - $Percent)

 

Hopefully that was a bit more helpful and enlightening than what you found your friend to be? :)

 

PS: I know I've done a couple of unnecessary steps in here, and I did them in an attempt to further break down the process and make it easier to understand.

Edited by Christian F.
Link to comment
https://forums.phpfreaks.com/topic/272988-php-and-algebra/#findComment-1404947
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.