So I have a counter. It is supposed to calculate the current amount of something. To calculate this, I know the start date, and start amount, and the amount to increment the counter by each second. Easy peasy. The tricky part is that the growth is not quite linear. Every day, the increment amount increases by a set amount. I need to recreate this algorithmically - basically figure out the exact value at the current date based on the starting value, the amount incremented over time, and the amount the increment has increased over time.
My target language is Javascript, but pseudocode is fine too.
Based on AB's solution:
var now = new Date();
var startDate1 = new Date("January 1 2010");
var days1 = (no开发者_JAVA百科w - startDate1) / 1000 / 60 / 60 / 24;
var startNumber1 = 9344747520;
var startIncrement1 = 463;
var dailyIncrementAdjustment1 = .506;
var currentIncrement = startIncrement1 + (dailyIncrementAdjustment1 * days1);
startNumber1 = startNumber1 + (days1 / 2) * (2 * startIncrement1 + (days1 - 1) * dailyIncrementAdjustment1);
Does that look reasonable to you guys?
It's a quadratic function. If t
is the time passed, then it's the usual at2+bt+c, and you can figure out a,b,c
by substituting the results for the first 3 seconds.
Or: use the formula for the arithmetic progression sum, where a1
is the initial increment, and d
is the "set amount" you refer to. Just don't forget to add your "start amount" to what the formula gives you.
If x0 is the initial amount, d is the initial increment, and e is the "set amount" to increase the incerement, it comes to x0 + (t/2)*(2d + (t-1)*e)
If I understand your question correctly, you have an initial value x_0
, an initial increment per second of d_0
and an increment adjustment of e
per day. That is, on day one the increment per second is d_0
, on day two the increment per second is d_0 + e
, etc.
Then, we note that the increment per second at time t
is
d(t) = d_0 + floor(t / S) * e
where S
is the number of seconds per day and t
is the number of seconds that have elapsed since t = t_0
. Then
x = x_0 + sum_{k < floor(t / S)} S * d(k) + S * (t / S - floor(t / S)) * d(t)
is the formula that you are seeking. From here, you can simplify this to
x = x_0 + S * floor(t / S) d_0 + S * e * (floor(t / S) - 1) * floor(t / S) / 2.
use strict; use warnings;
my $start = 0;
my $stop = 100;
my $current = $start;
for my $day ( 1 .. 100 ) {
$current += ($day / 10);
last unless $current < $stop;
printf "Day: %d\tLeft %.2f\n", $day, (1 - $current/$stop);
}
Output:
Day: 1 Left 1.00 Day: 2 Left 1.00 Day: 3 Left 0.99 Day: 4 Left 0.99 Day: 5 Left 0.98 ... Day: 42 Left 0.10 Day: 43 Left 0.05 Day: 44 Left 0.01
精彩评论