<?php
/**
* 我国古代数学家张丘建在《算经》一书中提出的数学问题:
* 公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,
* 用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,
* 问公鸡,母鸡,小鸡要买多少只刚好凑足100只鸡100文钱
*/
$start = microtime(true);
$res = [];
for ($x = 0; $x <= 20; $x++) {
for ($y = 0; $y <= 34; $y++) {
for ($z = 0; $z <= 100; $z++) {
$totalMomey = 5 * $x + 3 * $y + (1/3) * $z;
$totalNum = $x + $y + $z;
if ($totalMomey == 100 && $totalNum == 100) {
$tmp = [];
$tmp['x'] = $x;
$tmp['y'] = $y;
$tmp['z'] = $z;
$res[] = $tmp;
}
}
}
}
$end = microtime(true);
print_r($res);
echo $end-$start;
上面程序还有优化空间,你看出来了吗?