程序流程圖:
代碼實現:
$num = 100; // 給定正整數 $first = 1; $factor = 2; $res = []; $str = ''; while ($num > 1) { if ($num % $factor != 0) { $factor++; } else { if ($first == 1) { $first = 0; } else { $res[] = '×'; $str .= '*'; } $res[] = $factor; $str .= $factor; $num = $num / $factor; } } var_dump($res); // 數組 echo $str; // 字符串
運行結果: