Jump to content

Recommended Posts

hey guys wondering if one of you highly evolved incredible math thinking people can help me out with a math problem. im going to keep the example really simple.

 

If i have 100 bucks

and each level i want to buy increases to the power of X. and it starts at 10 (x equaling of how many levels I want to buy)

 

so

pow(10,x)

 

with out knowing what x is

how do I figure out the max amount of levels i can buy with 100 bucks?

i could create a loop i guess to make it count up to 100 and use a increment to use to see how many times it had to loop to reach that amount but isnt there a way to do this with math?

 

hope i explained it good enough. Reall appreicate if someone can help me out with this. Its a little beyound me or im making it harder then what it is lol.

10 to the power of 1 is 10 my friend.

 

If you've had advanced math in high school you'd probably heard of logarithms. In this case you need common logarithm  (base 10, decimal base) as opposed to natural (base e) or binary logarithm (base 2).

Logarithms are used to revert exponentiation, i.e. 10x = 100. To find out x, we use log(100) = x, and log(100) = 2.

 

The common logarithm function in PHP is log10() and natural is just log(), where you also can specify a base, default though is e.

 

Then use floor() to get the maximum level a person can afford.

 

Example: If you got 150 bucks, you're between 100 (102) and 1000 (103) and log(150) is 2.17609126 which when floored becomes 2. Thus you can maximum afford level 2.

silkfire thanks alot!

 

that was one of the functions i skiped over haha i was like log that cant be it haha.

 

yah 10 actually wont be the base i'll be using so i'll have to use log() instead of log10()

 

the power of php math functions are amazing ha :)

 

Thanks alot on the correction. I didnt realize that 10 to the power of 1 isnt 10x10

i should of known better cause if i square a number it gives me the results of what I thought 10 to the power of 1 was haha

another question

 

is there a php math function that does this

 

if i have 100 bucks

 

and each level cost 5 more then the last level

 

how many levels can I afford to buy

 

or backwards

 

10

+5 = 15

+5 = 20

+5 = 25

+5 = 30

 

total cost is 10 +15+20+25+30 = 100

I don't think there is a function for that, but a simple while loop will solve that for you.

 

<?php
$level = 1;
$cost = 10;
$money = 100;
while($money > $cost)
   {
       $money -= $cost; //remove cost from money left
       $cost += 5; //increase base cost
       ++$level; //increment your level
   }
echo $level;

 

That will output 5 levels for 100 bucks.

 

Edit:: Removed a pointless step.

I don't think there is a function for that, but a simple while loop will solve that for you.

 

<?php
$level = 1;
$cost = 10;
$money = 100;
while($money > $cost)
   {
       $money -= $cost; //remove cost from money left
       $cost += 5; //increase base cost
       ++$level; //increment your level
   }
echo $level;

 

That will output 5 levels for 100 bucks.

 

Edit:: Removed a pointless step.

if you chane many to 90 that will output 5 levels for 90 bucks.

I'm still having a huge problem. sigh been working hours on this lol. I just cant process this correctly in my brain.

I cant figure out how to keep it from buying more then the amount of levels i can afford.

below i have enough money to buy 2 levels. If i try to buy 2 or more it lets me. Hopefully someone can help me this is driving me crazy been working on it for 6 hours sadly but true.

 

I'm currently on level 2. Each level increases to the power of 3

$current_level = 2;
$next_level_cost = pow(3,$current_level + 1); // current level plus the next level to the power of 3
$money = 12;

while($money > $next_level_cost)   {       

	$money -= $next_level_cost; //remove cost from money left       
	++$next_level;
	$next_level_cost = pow(3,$next_level); //increase base cost 

++$levels_bought;  //increment your level  

}

i thought log10 is only if im using 10 as the 2nd  parameter?

 

This doesnt give me how many levels i can buy with 40 bucks with the first level costing 3.

to buy 3 levels with 40 bucks would cost 39 bucks

$money = 40;

$max = floor(log($money,3));

echo $max;

return = 3

 

trying to set this up so that if someone tried to buy 10 levels but didnt have enough money for it. then it will buy as many levels as it can.

I almost have my problem solved

my problem now is money left over is coming up as -11 not sure why. should be 1 left over

 

	$current_level = 0;
	$next_level = ($current_level + 1);

	$money = 40;
	$cost = 3;
	$x = 1;
	$input = 1;

	$max_level = 1;

while($cost < $money)   {       

	++$next_level;
	$cost += pow(3,$next_level);
	$money -= $cost;
	++$max_level;
	++$x;
}
echo "$money is how much money is left <br />"; // return is -11
echo "$max_level most level you can afford <br />"; // return is 3
echo "$cost cost is for the amount you can most buy <br />"; return is 39
echo "It looped $x amount of times"; return is 3

Pardon me, I meant log() in my earlier post. By the way I think your logic is flawed and you're mixing things up.

 

To calculate the product of exponents with the same base, we use the base raised to the power of the sums of its exponents: 31 * 32 * 33 * 34 = 3(1+2+3+4) = 310 = 59,049

To calculate the sums (as you want) you need to use this formula: (1 - b^N) / (1 - b) - n, where b = 3 (base), n = 1 (starting level), N = 4 (ending level).

Expanding: (1 - 3^4) / (1 - 3) - 1 = 39

 

Try using this formula in your loop then calculate the rest.

Okay try this code:

 

$cost_firstlevel = 3;		// base
$max_level = 1;

$money = $money_left = 40;
$cost = 0;

if ($money > $cost_firstlevel) {
   while ($cost <= $money) {
      $cost += pow($cost_firstlevel, $max_level);
      $money -= $cost;

      $max_level++;
   }

   $money_left = $money_left - ((1 - pow($cost_firstlevel, $max_level)) / (1 - $cost_firstlevel) - 1);
   $max_level--;
}
elseif ($money == $cost_firstlevel) {
   $cost = $cost_firstlevel;
   $money_left = 0;
}
else
   $max_level--;

echo "$money_left is how much money is left <br />"; // return is 1
echo "$max_level most level you can afford <br />"; // return is 3
echo "$cost cost is for the amount you can most buy <br />"; //return is 39

try

<?php

$cost_1st_level= 3;
$level_cost_diff = 10;
$total_money = 40;
$tmp = 2 * $cost_1st_level - $level_cost_diff;
$number_of_level_can_by = floor((-$tmp + sqrt($tmp * $tmp + 8 * $total_money * $level_cost_diff) ) / (2 * $level_cost_diff));
echo $number_of_level_can_by;

?>

Thanks alot guys for the help

I actually got it all working using 4 loops lol.

 

I use 2 differant resources to buy building levels for my game.

 

so what i did was. I created two loops to find the max amount of levels i can buy with each resource starting at level 0. then subtracted how many levels i already had. then i took the min of the two to find the most levels i can buy. cause it takes a differant amount of each resource for each level. ie (10 gold for level 1 and 5 silver for level 1)

 

Then i created a loop to calculate how much it cost to buy the levels i already had on the building.

Then i made a loop to figure out how much it cost to buy the min amount of levels plus the levels i already have

Then i simply took ("cost of the most i can buy plus the levels i have already" subtracted from " the cost of what was spent to buy the levels that already existed"

 

which gives me the cost to subtract from each resources

The loop below is figuring out the cost to buy the max building level starting at level 0 and then finding the min of the two

 

// for resources
while($homes_res_level_costs < $homes_res)   {       
                       $homes_res_level_costs += pow($homes_res_val,$homes_max_level_res + 1);
                       $homes_res -= $homes_res_level_costs;
             ++$homes_max_level_res;
}
// for naqahdah
while($homes_naq_level_costs < $homes_naq)   { 
                      $homes_naq_level_costs += pow($homes_naq_val,$homes_max_level_naq + 1);
                      $homes_naq -= $homes_naq_level_costs;
            ++$homes_max_level_naq;
}	


                  // putting the two max amounts in the  array to find the lowest
$homes_most_res = ($homes_max_level_res - $homes);
$homes_most_naq = ($homes_max_level_naq - $homes);

$homes_most = min(array_filter(array($homes_most_res, $homes_most_naq)));

 

 

I really wanted to figure out how to combine the two loops i made for resouces and naqahdah.  so the loop stops when you run out of naqahdah or resources first.

 

so i need a statement that combines it like this below

but using OR doesnt make it stop at the first one that maxes out would it? sorry hard to explain this. Does OR use the first while statement that comes true?

while($homes_res_level_costs < $homes_res || $homes_naq_level_costs < $homes_naq) {

 

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.