/** * factorial($num) 计算阶乘 * @param string $num * @return string $total */ function factorial($num) { if (empty($num)) { return ‘输入不能为空!‘; } elseif ($num < 0){ echo $num.‘没有阶乘!‘; } elseif ($num == 0 || $num == 1) { echo $num.‘的阶乘等于其本身!‘; } else { static $total = 1; $total *= $num; $num--; if ($num != 1) { factorial($num); } return $total; } }
时间: 2024-12-22 10:36:52