除数的逻辑php条件总和

时间:2019-03-05 13:09:39

标签: php algorithm function logic

我有一个从1到100的数字数组,我需要找到一个应用两个条件的数字列表,第一个除数之和大于自身,第二个条件没有除数的子集不能对数字本身求和 为此,我有php代码

  $arrayNumbers = [];
   //array numbers 1 - 100
    $a = 1;
    $b = 100;

        for ($n = $a; $n <= $b; $n++) {
            $conditionOne = false; // Condition one: sum of the divisors greater than ifself
            $conditionTwo = false; // Condition two: No subset of those divisors sums itself
            $multiples = [];
            $subset = [];
            // Get sum of multiples divisors
            for ($i = 1; $i < $n; $i++) {
                if ($n % $i == 0) 
                $multiples[]= $i;
            }
            //Condition one sum of the divisors greater than itself
            if (array_sum( $multiples ) > $n) 
                $conditionOne = true;

            foreach ($multiples as $number) {
                if ($number % 2 == 0) $subset[]= $number;
            }
            // Condition tow sum of the divisors greater than itself
            if (array_sum($subset) > $n) 
                $conditionTwo = true;
            // If first ondition one match with second condition two
            if ($conditionOne && $conditionTwo) 
                $arrayNumbers[] = $n;
        }

    echo implode('</br>$arrayNumbers ); 

此php逻辑正确吗?预先感谢。

0 个答案:

没有答案