  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # How to divide a number into multiple parts(not equal) so that there sum is equal to input?  » algorithm » How to divide a number into multiple parts(not equal) so that there sum is equal to input?

By : Ngai Fong
Date : November 24 2020, 05:44 AM
around this issue Simple approach exploits "trial and error" method. Suitable for reasonable small input values.
Note - it might work long time when n is close to p*maxx. If such case is possible, it would more wise to distribute "holes" rather than "ones" (the second code) code :
``````import random

def randparts(n, p, maxx):
lst =  * p
while n > 0:
r = random.randrange(p)
if lst[r] < maxx:
n -= 1
lst[r] += 1
return lst

print(randparts(20, 10, 4))

>>>  [2, 0, 3, 2, 4, 2, 1, 3, 0, 3]
``````
``````def randparts(n, p, maxx):
if p * maxx  >=  n * 2:
lst =  * p
while n > 0:
r = random.randrange(p)
if lst[r] < maxx:
n -= 1
lst[r] += 1
else:
lst = [maxx] * p
n = maxx * p - n
while n > 0:
r = random.randrange(p)
if lst[r] > 0:
n -= 1
lst[r] -= 1
return lst

print(randparts(16, 10, 4))
print(randparts(32, 10, 4))

>> [2, 0, 0, 3, 4, 0, 0, 3, 2, 2]
>> [3, 4, 4, 4, 4, 0, 3, 3, 4, 3]
`````` ## Divide a number into equal parts and store their cumulative sum

By : Raju Kulayappa
Date : March 29 2020, 07:55 AM
Hope that helps Simple question, how do I get every option when dividing a number? For example: , It's rather trivial to make.
code :
``````<?php

/**
* @param int \$number  The beginning number
* @param int \$divider The number dividing by
*
* @return array
* @throws Exception   In case \$number is not divisible by \$divider
*/
function get_number_sequence(\$number, \$divider) {
//In case \$number is not divisible by \$divider, throw an Exception.
if (\$number % \$divider !== 0) {
throw new Exception("\$number is not divisible by \$divider");
}
//Return an array from \$divider to \$number in steps of \$divider.
\$result = range(\$divider, \$number, \$divider);
return \$result;
}

/*
* Testing begins
*/

try {
echo "<pre>";
echo implode(", ", get_number_sequence(24, 4)) . PHP_EOL;
echo implode(", ", get_number_sequence(24, 6)) . PHP_EOL;
echo implode(", ", get_number_sequence(24, 5)) . PHP_EOL;
echo "</pre>";
}
catch (Exception \$e) {
echo "Invalid: " . \$e->getMessage();
}
`````` ## How to divide a number into multiple parts so that the resulting sum is equal to the input?

By : Salebricha SanDeep
Date : March 29 2020, 07:55 AM
around this issue I am trying to divide a number into multiple parts so the sum of the part are equal to the input number. , I tested with your supplied values. May need some tweaking:
code :
``````static IEnumerable<decimal> SplitValue2(decimal value, int count)
{
if (count <= 0) throw new ArgumentException("count must be greater than zero.", "count");
var result = new decimal[count];

decimal runningTotal = 0M;
for (int i = 0; i < count; i++)
{
var remainder = value - runningTotal;
var share = remainder > 0M ? Math.Max(Math.Round(remainder / ((decimal)(count - i)), 2), .01M) : 0M;
result[i] = share;
runningTotal += share;
}

if (runningTotal < value) result[count - 1] += value - runningTotal;

return result;
}
`````` ## Divide number into equal parts

By : surcol
Date : March 29 2020, 07:55 AM
it fixes the issue not sure how to describe this.. but here goes..
code :
``````//Your starting values
\$a = 4520;
\$b = 50;

/**
* Array and FOR loop where you add the
* second value each time to an array,
* until the second value exceeds the
* first value
*/
\$c = array();
for(\$n = \$b; \$n <= \$a; \$n += \$b) {
array_push(\$c,\$n);
}

/**
* If the new final value is less than
* (not equal to) the original number,
* add the original number to the array
*/
if(\$n > \$a) array_push(\$c, \$a);

/**
* If the new running total is greater
* than (and not equal to) the original
* number, find the difference and add
* it to the array
*/
#if(\$n > \$a) array_push(\$c, \$n-a);

//Print values
echo '<pre>';
print_r(\$c);
`````` ## Divide a number - Multiple levels - Sum of all numbers equal to input number - Python

By : user5832331
Date : March 29 2020, 07:55 AM
code :
``````number = 500
divide=[5,6,9]
interim = []
for i in divide:
j=(number/sum(divide))*i
interim.append(j)
print (number, j)

for j in interim:
for i in divide:
k = j/sum(divide)*i
print (j, k)
``````
``````500 125.0
500 150.0
500 225.0
125.0 31.25
125.0 37.5
125.0 56.25
150.0 37.5
150.0 45.0
150.0 67.5
225.0 56.25
225.0 67.5
225.0 101.25
`````` ## Divide number into equal parts and populate into array

Date : March 29 2020, 07:55 AM
wish help you to fix your issue Given numbers number, n and interval I'm trying to divide them into equal intervals and pre-populate them into an array. , It looks like you need a solution for three parts, where the
code :
``````function getParts(sum, n, length) {
var left, right;
if (n * length === sum) return Array.from({ length }, _ => n);
if (n * length < sum) {
left = Math.floor(sum / n);
} else {
left = n;
n = Math.floor(sum / left);
}
right = (sum - left * n) / (length - n);
return Array.from({ length }, (_, i) => i < n ? left : right);
}

console.log(getParts(8, 2, 4).join(' '));  // 2 2 2 2
console.log(getParts(56, 8, 7).join(' ')); // 8 8 8 8 8 8 8
console.log(getParts(9, 2, 3).join(' '));  // 4 4 1
console.log(getParts(10, 2, 3).join(' ')); // 5 5 0
console.log(getParts(9, 4, 3).join(' '));  // 4 4 1
console.log(getParts(8, 5, 4).join(' '));  // 5 1 1 1``````
``````function getParts(sum, length) {
var left = Math.ceil(sum / length),
right = Math.floor(sum / length),
first = (sum - right * length) / right;

return Array.from({ length }, (_, i) => i < first ? left : right);
}

console.log(getParts(8, 5).join(' ')); // 2 2 2 1 1
console.log(getParts(8, 4).join(' ')); // 2 2 2 2
console.log(getParts(8, 3).join(' ')); // 3 2 2
console.log(getParts(9, 3).join(' ')); // 3 3 3`````` 