-5

I would like to calculate the spending factor in a rule where we spend Nth time the previous payment done

Here is an example of spending.

firstPaymentAmount=10
SpendingFactor=5
PaymentCount=4

payment1:    10
payment2:    50       (=  10 x 5)
payment3:   250       (=  50 x 5)
payment4:  1250       (= 250 x 5)

At the end we get the sum of all payment made and we have :

10 + 50 + 250 + 1250 = 1560

I would like to know the formula that let me retrieve the spending factor (=5 here) by only knowing these parameters:

paymentCount  = 4
initalPayment = 10 
totalPaid     = 1560

By knowing the formula to calculate the spendingFactor, I will then be able to know the amount and detail of each payment.

Jean F.
  • 127
  • 8
  • 4
    Looks like more of a math question than a programming one. – Don't Panic Dec 07 '17 at 18:04
  • 1
    Or a question from an interview – ka_lin Dec 07 '17 at 18:04
  • yeah more math question, it's not for an interview but a personal PHP project. Is it okay if I post the question also at stackexchange too? – Jean F. Dec 07 '17 at 18:06
  • `$sum = 0; $payment = [5, 50, 250, 1250]; for ($i = 0; $i < count($payment); $i++) { $sum += $payment[i];}` Where $initialPayment = $payment[0], $paymentCount = count($payment) -1 – Zyigh Dec 07 '17 at 18:10
  • I'm seeking spendingFactor here. I'm not supposed to know the details ($payment = [10, 50, 250, 1250];). I just know the total payment made (=1560), the first payment amount (=10) and how many payment were made (=4) By knowing the spending factor, I will be able to calculate that array thenafter – Jean F. Dec 07 '17 at 18:12
  • Google *geometric series*; you will find that the *geometric ratio* cannot be solved for directly; you could use an iterative solution like *Newton-Raphson* – meowgoesthedog Dec 07 '17 at 21:10

1 Answers1

0

I've came with a solution. It gets an approximation of the spending factor because there is no direct formula.

Getting the exact spending factor is too CPU intensive. So, I'm calculating a near approximation first. This value will always be above the solution. I then decrease that number until I'm getting below the total paid amount.

Here is a PHP sample of what I've done.

$paymentCount   = 4;
$initialPayment = 10; 
$totalPaid      = 1560;

//----- precalculate the factor based on total payment for faster computation
//----- this predefined factor will always be above our final factor

$estimatedSpendingFactor = exp(log($totalPaid) / $paymentCount);

//----- find the estimated spending factor

do
{
    $estimatedSpendingFactor -= 0.0001;

    $y = $initialPayment * (pow($estimatedSpendingFactor, $paymentCount) - 1) 
                         / ($estimatedSpendingFactor-1);
}
while ($y > $totalPaid);

//-----

printf("The spending factor is %f\n", $estimatedSpendingFactor);

the output will be :

The spending factor is : 5.000000
Jean F.
  • 127
  • 8