How to do Partition algorithm in php

I tried getting partition of numbers in PHP.

For example, number 10, divide like 4, 3, 3. if 14 then 5, 5, 4

this type of partition

Upvotes: 0

Views: 223

Answers (1)

Sriram C G
Sriram C G

Reputation: 931

Try this snippet :

<?php

$number = 14;
$limit = 3;


$rem = $number%$limit;

$divi = floor($number/$limit);

$part = array();

for($i=0; $i<$rem; $i++) {
   $part[$i] = $divi+1;
}

for($i=$rem; $i<$limit; $i++) {
   $part[$i] = $divi;
}

//$part[$rem+1] = $divi;

print_r($part);

?>

Upvotes: 2

Related Questions