Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing an even number into N parts each part being a multiple of 2

Let's assume I have the number 100 which I need to divide into N parts each of which shouldn't exceed 30 initially. So the initial grouping would be (30,30,30). The remainder (which is 10) is to be distributed among these three groups by adding 2 to each group in succession, thus ensuring that each group is a multiple of 2. The desired output should therefore look like (34,34,32).

Note: The original number is always even.

I tried solving this in Python and this is what I came up with. Clearly it's not working in the way I thought it would. It distributes the remainder by adding 1 (and not 2, as desired) iteratively to each group.

num = 100
parts = num//30  #Number of parts into which 'num' is to be divided

def split(a, b):
  result = ([a//b + 1] * (a%b) + [a//b] * (b - a%b))
  return(result)

print(split(num, parts))

Output:

[34, 33, 33]

Desired output:

[34, 34, 32]
like image 671
accibio Avatar asked Dec 17 '21 11:12

accibio


People also ask

How do you divide a number into N equal parts?

If the number is being split into exactly 'N' parts then every part will have the value X/N and the remaining X%N part can be distributed among any X%N numbers. Thus, if X % N == 0 then the minimum difference will always be '0' and the sequence will contain all equal numbers i.e. x/n.

How do you check if a number can be divided into two even numbers?

Naive approach: Check all number pairs upto N, such that they both are even and they both sum to N. If possible, print Yes, else No.

Can an odd number be split into two even numbers?

An odd number cannot be split into two equal (or even) groups. 7 doughnuts cannot be shared evenly between two people. That is because 7 is an odd number. Tip: the last digit of an odd number is always 1, 3, 5, 7, or 9.

Can 8 be divided into two even parts?

Given a number N, the task is to check if this number can be divided into 2 even parts. Explanation: 8 can be divided into two even parts in two ways, 2, 6 or 4, 4 as both are even. Naive approach: Check all number pairs upto N, such that they both are even and they both sum to N. If possible, print Yes, else No.

Can an integer be divided into two different even parts?

The task is to decide whether the integer can be divided into two unequal positive even parts or not. Explanation: 8 can be divided into two different even parts i.e. 2 and 6. Explanation: 5 can not be divided into two even parts in any way. Explanation: 4 can be divided into two even parts, 2 and 2. Since the numbers are equal, the output is NO.

How do you divide a number into P parts?

To distribute the number n into p parts, you would calculate the “truncating integer division” of n divided by p, and the corresponding remainder. d = ⌊ n p ⌋, r = n mod p = n − p d.

What is the expected output when dividing a number into parts?

10 3 I am trying to divide a number into multiple parts so the sum of the part are equal to the input number. If I have 3.99 and if I need to divide into two parts, the expected output is 2 and 1.99 (2+1.99=3.99)


Video Answer


2 Answers

Simplified problem: forget about multiples of 2

First, let's simplify your problem for a second. Forget about the multiples of 2. Imagine you want to split a non-necessarily-even number n into k non-necessarily-even parts.

Obviously the most balanced solution is to have some parts be n // k, and some parts be n // k + 1.

How many of which? Let's call r the number of parts with n // k + 1. Then there are k - r parts with n // k, and all the parts sum up to:

   (n // k) * (k - r) + (n // k + 1) * r
== (n // k) * (k - r) + (n // k) * r + r
== (n // k) * (k - r + r) + r
== (n // k) * k + r

But the parts should sum up to n, so we need to find r such that:

n == (n // k) * k + r

Happily, you might recognise Euclidean division here, with n // k being the quotient and r being the remainder.

This gives us our split function:

def split(n, k):
    d,r = divmod(n, k)
    return [d+1]*r + [d]*(k-r)

Testing:

print( split(50, 3) )
# [17, 17, 16]

Splitting into multiples of 2

Now back to your split_even problem. Now that we have the generic function split, a simple way to solve split_even is to use split:

def split_even(n, k):
    return [2 * x for x in split(n // 2, k)]

Testing:

print( split_even(100, 3) )
# [34, 34, 32]

Generalisation: multiples of m

It's trivial to do the same thing with multiples of a number m other than 2:

def split_multiples(n, k, m=2):
    return [m * x for x in split(n // m, k)]

Testing:

print( split_multiples(102, 4, 3) )
# [27, 27, 24, 24]
like image 193
Stef Avatar answered Nov 15 '22 08:11

Stef


This solution is not very clear and easy to follow but it does not need any loops.

Full code:

def split(a,b):
     lower = (a//b//2) * 2
     num = a % (b*2) // 2
     return [lower + 2] * num + [lower] * (b - num)

Explanation:

  • First get the value of all parts: We round the result of the division (value // parts) down to the next even value ((x // 2) * 2)
  • To get the number of higher values: We use the remainder of the division of a in double as many parts and divide it by two to compensate the multiplication
  • last: higher numbers are just lower + 2 times the computed number of higher values and lower numbers are filling the other spaces
like image 44
Akida Avatar answered Nov 15 '22 09:11

Akida