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)};
}
}
I will provide one. . . .
First perform the factorization algorithm on the numbers
Then filter the result set and the result set that does not meet the requirements.