Reddit Ranking Algorithm - Rising - php

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.

Related

How to display number in percentage

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

Php Calculate percentage difference between two numbers

I am trying to find the difference of two numbers in percentage. The actual scenario is iam getting a total value of user data for this week and for last week. Now i need to see the performance difference in percentage with this weeks data and last weeks data. Following is my code which i am trying. But at times either any of the data will be zero and am getting an error "Division by zero". How to handle that?
$this_week_cust=$row["cust_count_new"];
$last_week_cust=$row["cust_count_old"];
$percentChange = (1 - $last_week_cust / $this_week_cust) * 100;
You can check if $this_week_cust is zero and just set the change to 100%
$this_week_cust=$row["cust_count_new"];
$last_week_cust=$row["cust_count_old"];
if ( $this_week_cust == 0 ) {
$percentChange = -100;
}
else {
$percentChange = (1 - $last_week_cust / $this_week_cust) * 100;
}
Altough the change would be 0 if $last_week_cust was also 0, so perhaps
if ( $this_week_cust == 0 ) {
$percentChange = ($last_week_cust==0)?0:-100;
}
I think this will help you.
$original= 1000;
$current = 5;
$diff = $current - $original;
$more_less = $diff > 0 ? "More" : "Less";
$diff = abs($diff);
$percentChange = ($diff/$original)*100;
echo "$percentChange% $more_less agaist $original";
try this
if $this_week_cust!=0 that time only calculation will perform otherwise not do anything
$this_week_cust=$row["cust_count_new"];
$last_week_cust=$row["cust_count_old"];
if($this_week_cust!=0){
$percentChange = (1 - $last_week_cust / $this_week_cust) * 100;
}
You can get the desired result by checking $this_week_cust, if is it return zero, null or empty set $this_week_cust to 100;
$percentChange=100;

Regular savings account interest calculation php

OK, I've had the same problem for a few weeks now and cant perfect it.
Aim
To build a regular deposit savings account system where it prints out the total balance at the current time.
Problem
The current equation I have:
If the interest is 6% with the user paying in 200 a month with compound being each month the balance would be after 6 months 1,220.61
I am getting 1217.13
I have tested different lengths of time and many different online calculators, my calculation is always less.
My code
<h2>Total Balance To Date</h2>
<?php
$p = 0; // Starting amount
$i = 0.06; // Interest rate
$c = 12; // compound frequency set to monthly
$n = 6/12; // Current time invested set to 6 months
$r = 200; // Monthly investment is 200
$x = $i / $c;
$y = pow((1 + $x), ($n * $c));
if($p!=0)
{
$vf = $p * $y + ($r * ($y - 1) / $x);
}
else
{
$vf = 1 + $y + ($r * ($y - 1) / $x);
}
?>
<p>£<?php echo round($vf, 2, PHP_ROUND_HALF_UP); ?></p> // Comes out at 1217.13
LINK to sandbox https://3v4l.org/9X7OH
Setting
q = pow(1.06 , 1.0/12) = 1.0048675505653430
and computing
200*(q+q^2+q^3+q^4+q^5+q^6) = 200*q*(q^6-1)/(q-1)
gives the result
1220.61037336530790
which is obviously what the online calculators worked with. It is slightly wrong, as for the nominal interest rate, the monthly compound factor should be
q = 1 + 0.06/12 = 1.005
resulting in a balance after 6 months of
1221.1758776293781
As you see, you got the formula almost right, it should be
$vf = $p * $y + ($r * (1 + $x) * ($y - 1) / $x);
since the rate is deposited at the start of the month, so the first rate gets compounded as r*(1+x)^6 and the last rate as r*(1+x). However, the second formula in the else branch does not make sense at all.

php, how to iterate through numbers and add as we go along and calculate new numbers

I wrote a simple code to calculate a math equation, but I would like to iterate through numbers from $start to $end. I am making a game, and this page will calculate the amount of experience it takes to reach the next level and insert it into the database. What would be the best way to iterate from $start to $end and calculate the amount of exp needed for that level?
Code:
<?php
$start = 1;
$end = 100;
$level = $start++;
$l = $level - 1;
$exp = ((40*($l * $l)) + (360 * $l));
?>
As it sits right now it calculates the first level but i cannot for the life of me figure out how to make it go through til it reaches $end.
$exp = 0;
for($level = $start; $level <= $end; $level++){
$exp += 40 * $l ** 2 + 360 * $l;
}
Actually, we can use mathematics to make this faster by generalizing the experience level required. Since your experience function is the summation of a quadratic function:
f(n)
= S[1 100] 40n^2 + 360n
= 40n (n + 1) (2n + 1) / 6 + 360n (n + 1) / 2
In PHP:
40 * $level * ($level + 1) * (2 * $level + 1) / 6 + 360 * $level * ($level + 1) / 2
Or simplify it further if you like.
This is definitely faster than calculating a loop 100 times.
If $start is not 1, simply use f(end) - f(start - 1).
You have to calculate the needed xp for every single level, so you should put the calculation code inside a loop that starts at your lowest level and proceeds until it hits the top limit / end level. You can choose from two different loop types in PHP, the for-loop and the while-loop.
Personally, I would choose the while-loop for this specific "problem", but that is a thing everyone has to decide on his own. The code for your calculator would look like this:
// Create an extra variable to store the level for which you are currently calculating the needed xp
$i = $start;
// The while-loop (do this code until the current level hits the max level as specified)
while($i <= end) {
// use $i to calculate your exp, its the current level
// Insert code here...
// then add 1 to $i and do the same again (repeat the code inside loop)
$i++;
}
Here are some links to the documentation of php:
while-loop
for-loop

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