0

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

2
  • what is this ?? 10 could be {1,1, 8}, {2,2,6}, {3,3,4}, {4, 4,2}, {5,5,0} You are the one who define the rule .. Commented Feb 16, 2017 at 11:00
  • It is not related to php. It is question about general algorithm for partition of numbers. Add tags to your question relating algorithms. You can read about partition here en.wikipedia.org/wiki/Trial_division and one of simple and basic algorithm here en.wikipedia.org/wiki/Trial_division Commented Feb 16, 2017 at 11:01

1 Answer 1

2

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);

?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.