How to factorize a number into PHP – Easy Way

In number theory, integer factorization is the breaking down of a composite number into smaller divisors, which when multiplied together equal the original number. By the fundamental theorem of arithmetic, every positive integer number, greater than one has a unique prime factorization.

Here’s how to factorize a number into php (two number integer factorization) :

<?php

function factor_number($number) {
   $s = array(); $f = array();
   $n2 = $number/2;
   for ($i=1; $i<=$n2; $i++) {
    if ($number % $i == 0) {
      $k = $number / $i;
      $sum = $i + $k;
      if (!in_array($sum, $s)) {
        $f[] = $i." * ".$k;
        $s[] = $sum;
      }
    }
   }
   return $f;
}

echo '<pre>';
print_r(factor_number(6552));
echo '</pre>';

And the result of the script is as follows:

Array
(
    [0] => 1 * 6552
    [1] => 2 * 3276
    [2] => 3 * 2184
    [3] => 4 * 1638
    [4] => 6 * 1092
    [5] => 7 * 936
    [6] => 8 * 819
    [7] => 9 * 728
    [8] => 12 * 546
    [9] => 13 * 504
    [10] => 14 * 468
    [11] => 18 * 364
    [12] => 21 * 312
    [13] => 24 * 273
    [14] => 26 * 252
    [15] => 28 * 234
    [16] => 36 * 182
    [17] => 39 * 168
    [18] => 42 * 156
    [19] => 52 * 126
    [20] => 56 * 117
    [21] => 63 * 104
    [22] => 72 * 91
    [23] => 78 * 84
)
byrev Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *