How to decompose an integer into the product of prime factors using PHP?
迷茫
迷茫 2017-05-16 13:07:43
0
2
613

For example, given a number 90, the result is "233*5";

I also got the result myself, but I always thought it was too cumbersome. I would like to ask the experts to see if there are any other ideas

//Determine whether it is a prime number. If it is a prime number, it returns 1, if not, it returns 0

function checkSS($num){
    if($num>0 && is_numeric($num) && is_int($num)){
        $flag = 1;
        for($i=2;$i<$num;$i++){
            if($num % $i == 0 && $num!=2){
                $flag = 0;
            }
        }
    }else{
        echo "Please enter a non-zero integer";
        exit;
    }
    return $flag;
}

//Decompose a non-zero integer into the product of prime factors

function splitNum($n){
    if(checkSS($n)){return $n."*1";}
    for($i=2;$i<abs($n);$i++){
        if($n % $i == 0 && checkSS($i)){
            $arr[] = $i; //Get the group of all non-repeating prime factors of the number
        }
    }
// var_dump($arr);exit;
    $res = array_product($arr);//The product of all prime factors of the number
 if($res == $n){
     return implode('*',$arr); //If the result is equal to the original number, use the * sign to split the array into a string to get the result. For example: 30 = 2*3*5
 }elseif(checkSS(abs($n/$res))){
     return implode('*',$arr)."*".$n/$res;//If the original number divided by the result is a prime number, multiply it directly. For example: 90 = 2*3*5 *3
 }else{
     return implode('*',$arr)."*".splitNum($n/$res);//Otherwise, divide the original number by the result and decompose it again, such as: 180 = 2*3*5 *{6= (2*3)};
 }
}
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
phpcn_u1582

I will provide one. . . .

    $int = 111;
    
    if(!is_int($int) || $int === 0) {
        echo "wrong number!";die;    
    }
    
    if($int <= 2) {
        echo $int . "=" . $int;die;
    }
    
    $result = $int . '=';
    while($int%2 == 0) {
        $int     = $int/2;
        $result .= 2 . '*';
    }
    
    for($i = 3; $i <= $int; $i += 2) {
        while($int%$i == 0) {
            $int     = $int/$i;
            $result .= $i . '*';
        }
    }
    
    $result = trim($result, '*');
    
    echo $result;die;
小葫芦

First perform the factorization algorithm on the numbers
Then filter the result set and the result set that does not meet the requirements.

class Helper
{
    public function chechPrime($num)
    {
        for ($i = floor(sqrt($num)); $i > 1; --$i) {
            if ($num % $i == 0) {
                return false;
            }
        }
        return true;
    }

    public function getFactorNumber(array &$result, array &$list, $n, $startFactor)
    {
        if ($n == 1) {
            if (count($list) > 1) {
                $result[] = $list;
            }
            return;
        }

        for ($i = $startFactor; $i <= floor(sqrt($n)); ++$i) {
            if ($n % $i === 0) {
                array_push($list, $i);
                $this->getFactorNumber($result, $list, $n / $i, $i);
                array_pop($list);
            }
        }

        array_push($list, $n);
        $this->getFactorNumber($result, $list, 1, $n);
        array_pop($list);
    }

    public function primeFactorNumber($num): array
    {
        $result = [];
        $list = [];

        $this->getFactorNumber($result, $list, $num, 2);
        array_push($result, [$num, 1]);

        $result = array_filter($result, function ($value) {
            foreach ($value as $v) {
                if (!$this->chechPrime($v)) {
                    return false;
                }
            }
            return true;
        });

        return $result;
    }
}

$helper = new Helper();

$result = $helper->primeFactorNumber(90);

var_dump($result);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template