As Manny Ramirez mentions, this is like an exponential decay function; except it's not continuous, but discrete. You basically want to produce:
seq(3a/n*(1-r)^(i-1)) for i from 1 to n
where in your case
a = 10000
n = 10
r = necessary value such that sum(seq(...)) == a == 10000
The challenge is finding r. I used a = ∑3a/n*(1-r)^(i-1) for i from 1 to n
. Doing some algebraic manipulation, I simplified this as far as:
3(1-r)^n+r*n-3=0
But, honestly, without a computer algebra system, there's no easy way to solve that. Frankly, as much as I like PHP, it sucks at this kind of thing. There may be a module in Python to do this for you. That said...
- If you reliably have access to Excel, you can use the equation solver combined with the Excel API (http://office.microsoft.com/en-us/excel-help/define-and-solve-a-problem-by-using-solver-HP010072691.aspx). I used that and got the rate you did (more precisely .290271556501). Raise the tolerance a little if it's unable to solve. Also set the constraint that 0 < r < 1, otherwise you may receive multiple answers (or just an incorrect one if it stops trying after finding one).
- You can try using the API of an online solver such as Wolfram Alpha(http://products.wolframalpha.com/api/).
- There's another stackoverflow post on computer algebra in PHP here: How can you solve equations in PHP?
- You can write your own computer algebra app for PHP (obviously I don't recommend this one).
Once you've used one of these methods (or another) to find r, then you need to build the array:
function getUnits ($total, $days, $rate) {
$start = $total * 3 / $days;
$units = array();
for ($i = 1; $i <= $days; $i++) {
$units[] = $start * pow((1 - $rate), $i - 1);
}
return $units;
}
//try with the numbers we have here
$units = getUnits(10000, 10, 0.290271556501);
print_r($units);
echo array_sum($units);
This gives
>> Array ( [0] => 3000 [1] => 2129.185330497 [2] => 1511.1433905345 [3] => 1072.5014464679 [4] => 761.18478225207 [5] => 540.23449072289 [6] => 383.41978422523 [7] => 272.1239266649 [8] => 193.13409091071 [9] => 137.07275772865 )
>> 10000.000000004