How to display number in percentage - php

I wanted to know how I present a percentage, here is the code
//The number taken from the database
$minos = $ud['bnk']['gold'];
The number that is supposed to be in percent through the database (the database has a number and not a percentage for example 2)
$plus= $ud['bnk']['ent_level'];
And here is a simple calculation of X + 2% = Y
$sava = $minos + $plus;
I tried to do this, according to an internet guide, but it doesn't work for me, I want the number to be a percentage and not successful
function get_percentage($total, $number)
{
if ( $total > 0 ) {
return round($number / ($total / 100),$ud['bnk']['ent_level']);
} else {
return 0;
}
}
$minos = $ud['bnk']['gold'];
$plus = get_percentage(100,$ud['bnk']['ent_level']).'%';
$sava = $minos + $plus;

I solved the equation with what you gave me, thank you very much, the way is:
$minos = $ud['bnk']['gold'];
$plus = ($minos / 100) * $ud['bnk']['ent_level'];
$sava = $plus;
Thanks so much to everyone who helped

Related

Calculating event happening from percent

I couldn't find any answers, so I am asking here.
How can I calculate whether an event should be triggered or not based on percent?
Let me explain.
Let say an event has a 30% probability of occurring.
When I run the script (call a function) how can I know if that event falls into that 30% or not?
Basically, in the end, I have to return true or false from a function.
Hopefully, you understand what I mean.
I have my own solution, but I believe it isn't correct:
$evasion_percent = 30;
$did_evasion = false;
$my_evasion_number = mt_rand(0,100);
if ($my_evasion_number <= $evasion_percent) {
$did_evasion = true;
}
return $did_evasion;
Thanks.
Your solution is fine.
$evasion_percent will be greater than or equal to a random integer 30% of the time as there are 30/100 numbers (excluding 0) that will make your function return true.
ie. (30 desirable outcomes that make this function return true / sample space of 100) ~ 30%.
You need to get random from 1 to 100, not from 0 to 100, the rest looks fine.
<?php
function trigger_event($percent) {
$did_evasion = false;
$my_evasion_number = mt_rand(1, 100);
if ($my_evasion_number <= $percent) {
$did_evasion = true;
}
return $did_evasion;
}
You can also try it with large number of calls (1000, 100000 or even more) and see how part of triggered events is closed to your percentage:
$evasion_percent = 30;
$m = 0;
for($n = 1; $n <= 1000; $n++) {
$m += trigger_event($evasion_percent) ? 1 : 0;
}
echo $m / $n * 100;

Find the missing number in the given array of numbers using PHP

Am new to php i have faced an interview some days ago, and the interviewer asked a question like the following one.
The given array has 99 numbers, which contains the digits from 1 to 100
with one digit missing. Describe two different algorithms that finds you the missing number. The algorithm should optimize for low storage and fast processing. Output should show the execution time of each algorithm.
And i have searched google about it, and come to know its a common puzzle used to ask in interviews. I found out the answer like this way.
int sum = 0;
int idx = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
idx = i;
} else {
sum += arr[i];
}
}
// the total sum of numbers between 1 and arr.length.
int total = (arr.length + 1) * arr.length / 2;
System.out.println("missing number is: " + (total - sum) + " at index " + idx);
But the code is not in php,
Can u please help me to find out the php code and algorithm name. So i can improve my answer in the next interviews.
In PHP you can easily use some array functions and achieve that. Best way is,
$missing = array_diff(range(1,100),$array);
DEMO.
Another way to do it is by using the array_sum function and the knowledge that all numbers from 1 to 100 added together equals 5050.
$missing = 5050-array_sum($array);
Converted to PHP, it is nearly the same.
(Not tested)
Of course, there are better ways, like the one Rikesh posted, but this is the exact one you asked for:
$sum = 0
$idx = -1
for($i = 0; $i < count($arr); $i++){
if($arr[$i] == 0){
$idx = $i;
}else{
$sum += $arr[$i];
}
}
$total = (count($arr) + 1) * (count($arr) / 2);
echo "Missing: " . ($total - $sum) . " at index " . $idx;
$arr=range(1,99);
$j=1;
for($i=0;$i<100;$i++){
if(!in_array($j,$arr)){
echo $j.'is missing';
}
$j++;
}

Reddit Ranking Algorithm - Rising

I am using this algorithm based of Reddit and want to change it so it shows rising posts rather than hot. Which number do I change so that the time between posting and now have a greater influence on the returned score? I have tried changing a few numbers but still am not getting anywhere
Thanks heaps
function calculate_rank_sum($score, $created_at) {
$order = log10(max(abs($score), 1));
if ( $score > 0 ) {
$sign = 1;
} elseif ( $score < 0 ) {
$sign = -1;
} else {
$sign = 0;
}
$seconds = intval(($created_at - mktime(0, 0, 0, 1, 1, 1970))/86400);
$long_number = ($order + $sign) * ($seconds / 45000);
return round($long_number, 7);
}
This is the line which essentially calculates the score.
$long_number = ($order + $sign) * ($seconds / 45000);
If you wanted the time created to have a larger impact, I'd suggest reducing 45000, and/or offsetting ($order + $sign). This is a forumla which will require a lot of fine tuning to get it exactly the way you want it, so the best thing I'd suggest is to perform a lot of unit tests, and perform a lot of tweaks.
Your formula is wrong:
($order + $sign) * ($seconds / 45000)
You've added parentheses that should not be there. If you look at https://gist.github.com/zeuxisoo/882820, it should be:
$order + $sign * $seconds / 45000
But even that was based on the old reddit algorithm which was later fixed to:
sign * order + seconds / 45000
You can decrease 45000 to give the time component more weight.

Vote up down total max

Hey tried to search for similar but guess my english fail me lol, well here is what i need help with, im trying to make a vote system with up/down vote and wanna show it like this
5.3/10
but have no idea how to make total "5.3" not go over 100% = 10 here is my code so far
<?php
$Vote_up = 804;
$Vote_down = 942;
$total = $Vote_up + $Vote_down;
$result = 100;
echo number_format($total/$result,1,",",".") . "/10";
?>
result is 17,5/10
ps. new to php so be easy on me ^^
i am really bad at maths but i think this should be work
$Vote_up = 555;
$Vote_down = 555;
$total = $Vote_up + $Vote_down;
if( $total <= 0 ){
$score = -11;
} else {
$score = (($Vote_up / $total) + ($Vote_down / $total) * 10) * -1;
}
echo floor( $score + 11 ) . '/10';
if someone have a better solution, please i would like to know it - thanks!

Auto increase value php in file based on number entered

I have 5 different variables that I need to calculate. Right now these work, but what I want to do is have the price recalculate every 50 increase. Now, I can code into the form to only allow a maximum purchase of 50 at a time, but I want to allow for as many of each as the person has money for (that is what the numbers are). So I think what I need is a parabola style formula that auto increases every 50, but I don't know exactly what I'm looking for or how to implement it.
$resprice = 20000+($cityFetch['residential']*502)+($cityNum*1000);
$comprice = 18000+($cityFetch['commercial']*506)+($cityNum*1000);
$indprice = 23000+($cityFetch['industrial']*508)+($cityNum*1000);
$landprice = 600+($cityFetch['land']*.008);
$milprice = 25000+($cityFetch['military']*512)+($cityNum*1000);
EDIT: I was indicated that a loop will work for this.
EDIT3: Finally landed on this, Havvy helped me out on IRC
$cityCheck = mysql_query("SELECT * FROM cities WHERE nation='$nation'");
$cityNum = mysql_num_rows($cityCheck);
function determinePrice($baseCost, $scalar, $current, $bought) {
$min = $baseCost + $scalar * ($current + 1) + ($cityNum * 1000);
$max = $baseCost + $scalar * ($current + $bought) + ($cityNum * 1000);
$average = ($min + $max) / 2;
return $average * $bought;
}
$resprice = determinePrice(20000, 502, $cityFetch['residential'], $cityFetch['residential']);
$comprice = determinePrice(18000, 506, $cityFetch['commercial'], $cityFetch['commercial']);
$indprice = determinePrice(23000, 508, $cityFetch['industrial'], $cityFetch['industrial']);
$milprice = determinePrice(25000, 502, $cityFetch['residential'], $cityFetch['military']);
$landprice = 600+($cityFetch['land']*.008);
I use a step size of 1, instead of 50. It's more logical (one less magic number) to not give discounts based on how much you buy at a time, and it would be confusing to new players having a stepwise-linear function.
The reasoning behind this function can be found at http://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/.
function determinePrice($baseCost, $scalar, $current, $bought) {
$min = $scalar * ($current + 1) + ($cityNum * 1000);
$max = $scalar * ($current + $bought) + ($cityNum * 1000);
$average = ($min + $max) / 2;
return $average * $bought;
}
Using it would look something like this:
$resprice = determinePrice(20000, 502, $cityFetch['residential'], $resBought);
It won't work for land though, because land doesn't include the city price in it. For land, you can copy the function and remove the $cityNum * 1000 portions. You could also add in a $isLand parameter to the signature, and use a ternary operator to add 0 instead.
If you don't like that $cityNum has to already be declared with the proper value before declaring this function, you can always add it to the function signature, making it a pure function.
$count=1
if($count==50)
{
You increase the prices then
$count=1;
}
$resprice = 20000+($cityFetch['residential']*502)+($cityNum*1000);
$comprice = 18000+($cityFetch['commercial']*506)+($cityNum*1000);
$indprice = 23000+($cityFetch['industrial']*508)+($cityNum*1000);
$landprice = 600+($cityFetch['land']*.008);
$milprice = 25000+($cityFetch['military']*512)+($cityNum*1000);
$count++;

Categories