Is there a math functon in PHP that would provide safe division without fractions/decimal numbers? Meaning that it would "distribute" the amount as equally as possible, but only in whole numbers.
Some examples:
10/3 = [4, 3, 3]
11/3 = [4, 4, 3]
50/2 = [25, 25]
51/2 = [26, 25]
40/5 = [8, 8, 8, 8, 8]
40/3 = [14, 13, 13]
Meaning that 40/3
should give me three values 14
, 13
and 13
2
Answers
There is no such thing out of stock. You may write your own function for it.
Since there’s no working solution yet, here’s one:
The question seems to ask to split (partition) an integer number
s
into a set ofn
numbers that are eitherq1 = ceil(s/n)
orq2 = floor(s/n)
Since
q1 - q2 = 1
, that split can be generated by the formula:that is
with
n1 = s - q2 * n
,n2 = q1 * n - s
, andn1 + n2 = (q1 - q2) * n = n
Php function:
Example:
gives
More examples in sandbox.