【问题标题】:Solve a linear equation with PHP用 PHP 求解一个线性方程
【发布时间】:2018-03-21 02:06:58
【问题描述】:

情况是:

给定:3 个产品单位(数量、固定)、每个单位价格范围、价格变化的间隔以及 3 个产品的总价格。

目标:找出每种产品的单价以匹配给定数量的总价。

我用 PHP 编写的以下脚本,但它显示“HTTP ERROR 500”。我在这里犯了什么错误,你能告诉我吗?

<?php

$qty1 = 1500000;
$qty2 = 300000;
$qty3 = 500;

$minRate = 125.000;
$maxRate = 160.000;

$gTotal = 255050000.000;

for ($i=$minRate; $i <= $maxRate; $i=$i+0.001) { 
    $item1Total = $i * $qty1;

    for ($j=$minRate; $j <= $maxRate; $j=$j+0.001) {
        $item2Total = $j * $qty2;
        $twoItemTotal = $item1Total + $item2Total;
        if ($twoItemTotal < $gTotal) {
            for ($k=$minRate; $k <= $maxRate; $k=$k+0.001) { 
                $item3Total = $j * $qty3;
                $allItemTotal = $twoItemTotal + $item3Total;
                if($allItemTotal == $gTotal){
                    echo $i . "<br>" . $j . "<br>" . $k;
                    echo "<br>-";
                }
            }       
        }
    }
}
?>

【问题讨论】:

  • 您是否尝试将ini_set('display_errors','on'); 放在脚本的开头以查看实际错误在哪里?
  • 我认为您正在达到内存限制或其他东西..您正在循环 3x 35000 次....
  • 这个脚本需要很长时间执行,有3个嵌套循环,每个循环35000次,那么总迭代为:35000^3 = 4.2875e+13,尝试设置时间限制别的东西,但我不建议这样做,尝试改进你的代码。
  • 如果我们有算法,那么我们可以帮助您快速完成这项任务。
  • @Karkouch,谢谢,你是对的,这是一个巨大的循环。此任务找出单价表的条件是 - 单价必须有 3 个小数点(不截断以太,例如 NOT 0.926544 到 0.927 或 0.926!)并且 3 个小数的最后一位不能为 0(例如 NOT 0.980 或0.230)。最后,任务是找出满足等式的单价 X、Y、Z:1500000*X + 3300000*Y + 500*Z = 255050000.000 如果你还有更多,请告诉我。谢谢。

标签: php


【解决方案1】:

(我尝试在 stackoverflow 上使用 Latex ,但它不起作用!)

所以问题是:

找到满足等式1500000*X + 3300000*Y + 500*Z = 255050000.000的X、Y和Y,并且X、Y和Z必须在这个区间[125.000,160.000]?

所以让 X 和 Y 是 [ 125.000, 160.000 ] 中的任意数字,然后找到 Z?

使用简单的数学我们发现:

Z = (255050000.000 - 3300000*Y - 1500000*X)/500

Z 应大于或等于 125.000,且小于或等于 160.000。

意思是:

125.000 ≤ ( 255050000.000 - 3300000*Y - 1500000*X )/500 ≤ 160.000

然后

( 255050000.000 - 500*160.000 - 1500000*X )/3300000 ≤ Y ≤ ( 255050000.000 - 500*125.000 - 1500000*X )/3300000

让 Ymin = ( 255050000.000 - 500*160.000 - 1500000*X )/3300000

和 Ymax = (255050000.000 - 500*125.000 - 1500000*X)/3300000

Y 也应该大于或等于 125.000,并且小于或等于 160.000

所以 Ymin ≥ 125.000 且 Ymax ≤ 160.000

<?php
$qty1 = 1500000;
$qty2 = 300000;
$qty3 = 500;
$minRate = 125.000;
$maxRate = 160.000;
$gTotal = 255050000.000;
$solutions = array();
$N = 0;
$t_start = microtime(true);
for( $i = $minRate; $i <= $maxRate; $i += 0.001 ){
    $j_min = ( $gTotal - $qty3 * $maxRate - $i * $qty1 ) / $qty2;
    $j_max = ( $gTotal - $qty3 * $minRate - $i * $qty1 ) / $qty2;

    if( $j_min < $minRate || $j_max > $maxRate ) continue;

    for( $j = $j_min; $j <= $j_max; $j += 0.001 ){
        //
        $k = ( $gTotal - $j * $qty2 - $i * $qty1 ) / $qty3;
        $solutions[] = [ 'i'=>$i, 'j'=>$j, 'k'=>$k ];
        $N++;
        if( !($N % 10000) ){
            // Write into a csv file then reset, (because of memory limit)
            $solutions = array();
        }
    }
}
printf( '%d solutions found after %f seconds%s', $N, (microtime(true) - $t_start), PHP_EOL );

我已经在我的笔记本电脑上运行了这个脚本,这是我得到的:412292 solutions found after 0.166070 seconds

如果您想要高精度结果,请尝试BCMath

【讨论】:

  • 非常感谢@Karkouch 的努力。我会试试的,我会告诉你的!!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
相关资源
最近更新 更多