【问题标题】:How to calculate the multiplicator in a sum like sum += sum*(mulu^n)如何计算总和中的乘数,例如 sum += sum*(mul^n)
【发布时间】:2018-05-21 22:03:47
【问题描述】:

我想计算一条规则中的支出因子,我们在上一次付款完成时花费了第 N 次

这是一个支出示例。

firstPaymentAmount=10
SpendingFactor=5
PaymentCount=4

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

最后我们得到所有付款的总和,我们有:

10 + 50 + 250 + 1250 = 1560

我想知道让我只知道这些参数就可以检索支出因子(此处=5)的公式:

paymentCount  = 4
initalPayment = 10 
totalPaid     = 1560

通过了解计算支出因子的公式,我将能够知道每次付款的金额和详细信息。

【问题讨论】:

  • 看起来更像是一道数学题而不是编程题。
  • 或者采访中的一个问题
  • 是的,更多的数学问题,这不是面试,而是个人 PHP 项目。我也可以在 stackexchange 上发布问题吗?
  • $sum = 0; $payment = [5, 50, 250, 1250]; for ($i = 0; $i < count($payment); $i++) { $sum += $payment[i];} 其中 $initialPayment = $payment[0], $paymentCount = count($payment) -1
  • 我在这里寻找消费因素。我不应该知道细节($payment = [10, 50, 250, 1250];)。我只知道支付的总金额(=1560)、第一次支付的金额(=10)和支付了多少(=4)通过知道支出因素,我将能够计算该数组然后

标签: php math sum logarithm calculation


【解决方案1】:

我有一个解决方案。因为没有直接的公式,所以它得到了支出因子的近似值。

获取确切的支出因素过于占用 CPU。所以,我首先计算一个近似值。该值将始终高于解决方案。然后我会减少该数字,直到低于总支付金额。

这是我所做的 PHP 示例。

$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 spending factor is : 5.000000

【讨论】:

    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 2023-02-16
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 2023-01-12
    • 2017-05-16
    • 2013-11-04
    相关资源
    最近更新 更多