logo
down
shadow

How to divide a number into multiple parts(not equal) so that there sum is equal to input?


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 = [0] * 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 = [0] * 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]


Share : facebook icon twitter icon
Divide a number into equal parts and store their cumulative sum

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?

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

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

Divide a number - Multiple levels - Sum of all numbers equal to input number - Python


By : user5832331
Date : March 29 2020, 07:55 AM
wish of those help You just need some nested loops and an interim list for your results from your first looping pass.
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

Divide number into equal parts and populate into array


By : Admin
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
Related Posts Related Posts :
  • Interview stumper: friends of friends of friends
  • Computing the union and intersection of two unsorted arrays in O(nlogm) time
  • Find medians in multiple sub ranges of a unordered list
  • clustering words based on their char set
  • Efficient algorithm for finding repeating bit patterns?
  • confusion about dijkstra algorithm?
  • Connected components in organized 3d point cloud data
  • Worst case NlogN algorithm for Nuts and bolts matching
  • Is recursion or stack necessary for factorial time complexity algorithms?
  • hashing mechanism to hash an input (0 to 2^32 - 1) to a fixed possibly 12 character hash
  • Time Complexity Of This Code Snippet
  • Number of Triangles Containing The Point (0,0)
  • Finding a maximal sorted subsequence
  • Merging Binary search tree
  • Travel the checkerboard from top left to bottom right with lowest cost
  • Dynamic Programming solving an algorithm
  • How can a heap be used to optimizie Prim's minimum spanning tree algorithm?
  • Partitioning an array into 3 sets
  • Confusion about the combine step in Merge Sort Code
  • Efficient way to look up dictionary
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org