Example of decomposition prime factor operation implemented by PHP

  • 2021-10-25 06:18:56
  • OfStack

In this paper, an example is given to describe the decomposition prime factor operation realized by PHP. Share it for your reference, as follows:

Thoughts:

If you want to calculate the prime number of $num, collect at least the prime number array within $num and judge whether $num is in the prime number array:

If not, it is judged whether the current prime number $zhishu [$i] can divide the divisible number again, and if so, the prime number array "cursor" does not move (also let the current prime number $zhishu [$i] take modulus on the divisible number)

If you can't (divide the divisive number again), use the next prime number (let $i + +) to test for divisibility. If not ($num is in the prime array), it means that $num is itself a prime number, and it is directly echo. (If you are confused, look at the code directly.)

First, use a function to collect prime numbers in a certain range and put them into an array to return. (Take 1 out of the prime number and start with 2.) The code and comments are as follows:


// Get 1000 Prime number within 
function get_zhishu($num=1000){
  $num = floor($num);
  $zhishu = array();
  // Get first 1000 Prime number within 
  for($i=1; $i<=$num; $i++){
    $flag = true; // When flag For false Indicates that the number is not a prime 
    for($j=2; $j<$num; $j++){ //$j From 2 Start, because the divisor is 1 When, it can definitely be divisible 
      if($i>$j){ //$j If than $i It's still big, so taking the mold is definitely not 0 There is no comparative significance 
        $mod = $i%$j;
        if($mod == 0 ){ // When divisor $j For $i Within, if the module is 0 Indicates that the number is not a prime number 
          $flag = false;
        }
      }
    }
    if($flag){
      array_push($zhishu, $i);// If $flag Is true, then $i Is a prime number 
    }
  }
  array_shift($zhishu); // Put 1 Remove from the array of prime numbers 
  return $zhishu;
}
$zhishu = get_zhishu(1000); // Get 1 To 1000 Prime number within 

Then, from the array of prime numbers, take out one by one (starting with the smallest prime number) the prime numbers that meet the conditions (divisible). The code and comments are as follows:


/**
 * @param int $num  Prime number to be decomposed 
 * @param array $zhishu 1000 Array of prime numbers within 
 * @param int $i  Equivalent to an array of prime numbers " Cursor "
 * @author misaka Last summer 
 */
function fenjie_num($num, $zhishu, $i=0){
  if(!is_int($num) || $num<0){
    exit(' Please enter a positive integer! ');
  }
  if(in_array($num, $zhishu)){ // If the number is a prime number, the echo Yeah 
    echo $num,'<br />';
  }else{
    $ceil = ceil($num/$zhishu[$i]);
    if($ceil == ($num/$zhishu[$i])){
      echo $zhishu[$i],'<br />';
      if($ceil%$zhishu[$i]!=0){
        // If the current prime number can still be $ceil Divide exactly, continue to use the prime number ( No need $i++) , such as 90 Decompose into 2 , 3 , 3 , 5 , otherwise let $i++ Recursion 
        $i++;
      }
      fenjie_num((int)$ceil, $zhishu, $i);
    }else{
      fenjie_num($num, $zhishu, $i+1); // For 99 This is not a prime number, but it has no number 1 If it is divisible for the second time, use the following 1 Prime number ($i++) Test it 
    }
  }
}
fenjie_num(390, $zhishu);

Run results:

2
3
5
13

PS: Here are some computing tools recommended for your reference in the next step:

On-line decomposition prime factor calculator tool:
http://tools.ofstack.com/jisuanqi/factor_calc

On-line 1 yuan function (equation) solution calculation tool;
http://tools.ofstack.com/jisuanqi/equ_jisuanqi

Scientific calculator online _ advanced calculator online calculation:
http://tools.ofstack.com/jisuanqi/jsqkexue

Online Calculator _ Standard Calculator:
http://tools.ofstack.com/jisuanqi/jsq

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of PHP Mathematical Operation Skills", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Encyclopedia of PHP Array (Array) Operation Skills", "Tutorial on PHP Data Structure and Algorithm", "Summary of php Programming Algorithm" and "Summary of php Regular Expression Usage"

I hope this article is helpful to everyone's PHP programming.


Related articles: