首页 > 解决方案 > 代码无法区分php中的完美和非完美数字

问题描述

我应该写一个程序:

  1. 从 html 表单中获取一个正整数
  2. 然后在另一个 php 脚本中,检查这个整数是否是一个完美的数字并打印结果(结果是整数是否完美)。

完美数是一个正整数,等于其所有因子(不包括自身)之和。

我面临的问题:代码运行,但对于输入的每个正数,我得到相同的输出,即“是的,XXX 是一个完美的数字”,即使数字不是。

有人可以帮我找到代码中的错误吗?我无法在代码中找到问题。

#html form

<!DOCTYPE html>
<html>
<body>
    <form action="lab_2_3.php" method="POST">
        Enter positive integer:
        <input type="number" name="pos_num" min="1"/>
        <input type="submit"/>
    </form>
</body>
</html>
#php script

<!DOCTYPE html>
<html>
<body>
    <?php
        $pos_num = $_POST["pos_num"];
        
        function get_factors($pos_num){
            $myarray = array(); //creating new array
            for ($i = 1; $i <= ceil(($pos_num ** 0.5)); $i++){
                if ($pos_num % $i == 0){
                    $myarray[] = $pos_num; //appending array in php
                }
            
            return $myarray; }
        }
        
        function get_total($myarray){
            $total_val = 0;
            for ($j = 0; $j < count($myarray); $j++){
                $total_val += $myarray[$j];
            } return $total_val;
        }

        $a_list = get_factors($pos_num);
        $check_if_perf = get_total($a_list);
        if ($check_if_perf == $pos_num){

            echo "Yes, $pos_num is a perfect number";
        } 
        
        else{
            echo "No, $pos_num is not a perfect number";
        }
    ?>
</body>
</html>

标签: phphtmldebugging

解决方案


我尝试打印出一些变量,以查看该函数到底在使用什么:

function get_factors($pos_num){
    $myarray = array(); //creating new array
    $end = $pos_num ** 0.5;
    print "$end\n";
    for ($i = 1; $i <= $end; $i++){
        print "$i: " . $pos_num % $i . "\n"; 
        if ($pos_num % $i == 0){
            $myarray[] = $i; //appending array in php
        }
    
    return $myarray; }
}
// ===>
3.1622776601684
1: 0
No, 10 is not a perfect number

由此可见,for 循环没有按预期运行。所以第一步是清理代码和缩进。错误变得很明显:端括号放错了位置。

function get_factors($pos_num)
{
    $myarray = array(); //creating new array
    for ($i = 1; $i <= $pos_num ** 0.5; $i++) {
        print "$i: " . $pos_num % $i . "\n"; 
        if ($pos_num % $i == 0){
            $myarray[] = $i; //appending array in php
        }
    }
    
    return $myarray; 
}

// ==>
1: 0
2: 0
3: 1
No, 10 is not a perfect number

但是,get_factors()不能正确计算阶乘。由 设置的结束限制$pos_num ** 0.5不会收集所有阶乘。例如,

print 6 ** 0.5;

// ==>
2.4494897427832

为了解决这个问题,我们想要得到前半部分的阶乘(包括 n/2)

function get_factors($pos_num)
{
    $myarray = array(); //creating new array
    for ($i = 1; $i <= $pos_num/2; $i++) {
        if ($pos_num % $i == 0){
            $myarray[] = $i; 
        }
    }

    return $myarray; 
}

现在,将所有代码以标准格式放在一起,并附上解释原因的注释(我还把一些调试注释掉了):

<?php
// testing...
// $_GET["pos_num"] = 496;

// always start out with PHP logic, never HTML.  

// Deal with input:
// if submission is not destructive (ie, insert, update, delete), use GET.  
// if submission changes data state, use POST.  POST should always be followed
// with a redirect.  (Post-Redirect-Get pattern)
if(isset($_GET["pos_num"])) {
    $pos_num = $_GET["pos_num"];
    $a_list = get_factors($pos_num);
    $check_if_perf = get_total($a_list);

    $is_perfect = ($check_if_perf == $pos_num); // boolean
}

// functional logic:
function get_factors(int $pos_num) : array
{
    $myarray = array(); //creating new array
    $end = $pos_num /2;
    // $end = $pos_num ** 0.5;
    // print "$end\n";
    for ($i = 1; $i <= $end; $i++){
        if ($pos_num % $i == 0){
            $myarray[] = $i; //appending array in php
        }
    }
    return $myarray; 
}

function get_total(array $myarray) : int
{
    $total_val = 0;
    for ($j = 0; $j < count($myarray); $j++){
        $total_val += $myarray[$j];
    } 
    return $total_val;
}

// print_r(get_factors($pos_num));
// all done with logic, show presentation:
?>
<!DOCTYPE html>
<html>
<body>
    <?php if(isset($_GET["pos_num"])): ?>
    <div>
        <?= $is_perfect 
            ? "Yes, $pos_num is a perfect number"
            : "No, $pos_num is not a perfect number"
        ?>
    </div>
    <?php endif; ?>
    <form action="lab_2_3.php" method="GET">
        Enter positive integer:
        <input type="number" name="pos_num" min="1"/>
        <input type="submit"/>
    </form>
</body>
</html>

工作示例:http ://sandbox.onlinephpfunctions.com/code/d1e5823075c71272c67e09448e6302a4dffead39


推荐阅读