phpfan101 Posted December 9, 2009 Share Posted December 9, 2009 //level 2 - 500 //level 3 - 1500 //level 4 - 3500 //level 5 - 6000 function experience($L) { $a=0; $end=0; for($x=1; $x<$L; $x++) { $a += floor($x*500)/$x; } return floor($a); } function Get_The_Level($exp) { $a=0; $end =0; for($x=1; ($end==0 && $x<10000); $x++) { $a += floor($x*500)/$x; if ($exp >= floor($a)){ } else { return $x; $end=1; } } } function Get_Max_Exp($L){ $end=0; if ($exp == 0){ return 457; $end =1; } for($L=1;($L<10000 && $end==0);$L++) { $exp = experience($L); //echo $exp; if ($exp >= $user_class->exp){ return $exp; $end=1; } } } I'm currently using this snippet on my site... the problem? I DON'T GET ANY OF IT! i have it working properly, but can anyone simplify it for me please? Quote Link to comment https://forums.phpfreaks.com/topic/184487-ugh-math/ Share on other sites More sharing options...
Daniel0 Posted December 9, 2009 Share Posted December 9, 2009 The first two seem to be extremely convoluted ways of doing this: [math]experience(L) = 500L[/math] [math]L(experience) = \left\lfloor \frac{experience}{500} \right\rfloor + 1[/math] Or in PHP... function experience($L) { return 500 * $L; } function Get_The_Level($exp) { return floor($exp / 500) + 1; } The last one is broken, so it effectively always returns the integer 457 and makes an E_NOTICE about $exp not being set. Assuming it worked, I would expect it could also be written in a normal mathematical fashion. Quote Link to comment https://forums.phpfreaks.com/topic/184487-ugh-math/#findComment-973987 Share on other sites More sharing options...
rajivgonsalves Posted December 12, 2009 Share Posted December 12, 2009 After playing around alot I came to this conclusion <?php //level 2 - 500 //level 3 - 1500 //level 4 - 3500 //level 5 - 6000 function getExp($level) { if ($level==2) return 500; $product = 0; for ($i=3;$i<=$level;$i++) { $product += $i * 500; } return $product; } function getExpLevel($exp) { if ($exp<1500) return 2; $product = 0; $next = 0; for ($i=3;$i<=10;$i++) { $next += $i * 500; if ($exp > $product && $exp < $next) { return $i;} $product = $next; } } echo getExp(5); echo "<br />"; echo getExpLevel(5000); ?> Quote Link to comment https://forums.phpfreaks.com/topic/184487-ugh-math/#findComment-975882 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.