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]
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.
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.
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.
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.
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.
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.
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)
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]
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]
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]
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:
value // parts
) down to the next even value ((x // 2) * 2
)a
in double as many parts and divide it by two to compensate the multiplicationlower + 2
times the computed number of higher values and lower numbers are filling the other spacesIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With