4 回答
TA贡献446条经验 获得超754个赞
<?php
function bingbao($num) {
// 判断是否自然数
if($num<1 || !is_int($num)) return "wrong number";
$result = "$num: ";
$numList = [];
while($num!==1) {
// 判断是否偶数
if($num % 2 == 0) {
$num = $num / 2;
} else {
$num = $num * 3 + 1;
}
$numList[] = $num;
}
$result .= implode($numList, ' ');
return $result;
}
// 测试运行效果
for($i=1; $i<=10000; $i++) {
echo bingbao($i), '<BR><BR>';
}
TA贡献1条经验 获得超0个赞
<?php
function math($a){
$numArray = [];
if(is_numeric($a)){
while($a != 1){
if($a%2 == 0){
$a = $a/2;
$numArray[]=$a;
}else if($a%2 == 1){
$a = $a*3+1;
$numArray[]=$a;
}
}
}else{
echo 'is not number!';
}
echo $a;
var_dump($numArray);
}
- 4 回答
- 0 关注
- 1760 浏览
添加回答
举报