Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to convert a number(x) into array of numbers so that their sum is eq

ID: 648214 • Letter: I

Question

I am trying to convert a number(x) into array of numbers so that their sum is equal to the number (x), but the array length should be equal to the given length.

var x = 12

length 1: [12]          = 12          = 12
length 2: [6,6]         = 6+6         = 12      
length 3: [4,4,4]       = 4+4+4       = 12    
length 4: [2,2,4,4]     = 2+2+4+4     = 12  
length 5: [2,2,2,2,4]   = 2+2+2+2+4   = 12
length 6: [2,2,2,2,2,2] = 2+2+2+2+2+2 = 12
...
...
length 12: [1,1,1,1,1,1,1,1,1,1,1,1] = 1+1+1+1+1+1+1+1+1+1+1+1 = 12

Explanation / Answer

Given your desired total and a length, the most balanced array gets generated where the value of each entry is (total / length) or ((total / length) + 1);

function MakeSum( total, length )
{
var ret = [];
var portion = Math.floor(total / length);
var remainder = total % length;
var running_total = 0

if ( portion % 2 )
{
if(portion > 2)
{ portion -= 1; }
else
{ portion = 2; }
}

while( ((total - running_total) > (length - ret.length)) && ret.length < length )
{
ret[ret.length] = portion;
running_total += portion;
}
portion = 1
while(ret.length < length)
{
ret[ret.length] = portion;
running_total += portion;
}
if ( running_total < total && ret.length == length )
{
ret[0] += total - running_total;
}
return ret;
};
Original solution, which allowed non-one odds:

function MakeSum( total, length )
{
var ret = [];
var portion = Math.floor(total / length);
var remainder = total % length;

for (i=0; i < remainder; i++)
{
ret[ret.length] = portion + 1;
}
for (i=0; i < (length - remainder); i++)
{
ret[ret.length] = portion;
}
return ret;
};
Output:

12 => X[1] = {12}
12 => X[2] = {6,6}
12 => X[3] = {4,4,4}
12 => X[4] = {6,2,2,2}
12 => X[5] = {4,2,2,2,2}
12 => X[6] = {2,2,2,2,2,2}
12 => X[7] = {2,2,2,2,2,1,1}
12 => X[8] = {2,2,2,2,1,1,1,1}
12 => X[9] = {2,2,2,1,1,1,1,1,1}
12 => X[10] = {2,2,1,1,1,1,1,1,1,1}
12 => X[11] = {2,1,1,1,1,1,1,1,1,1,1}
12 => X[12] = {1,1,1,1,1,1,1,1,1,1,1,1}
If you need all of the possible arrays, that is possible too but it grows exponentially. As JavaScript recursion is strange, I'll present that answer in Python and the conversion to JavaScript is left as an exercise.

def MakeSums( total, length ):
if length == 1:
return [[total]]
rets = []
for X in range(1, total):
if (total - X) > 1 and (total - X) % 2:
continue
if length == 2 and X > 1 and X % 2:
continue
for possibles in MakeSums( X, length - 1 ):
rets.append( [total - X] + possibles )
return rets
Counts:

12 => X[1] = 1 possibilities
12 => X[2] = 5 possibilities
12 => X[3] = 13 possibilities
12 => X[4] = 34 possibilities
12 => X[5] = 70 possibilities
12 => X[6] = 106 possibilities
12 => X[7] = 133 possibilities
12 => X[8] = 126 possibilities
12 => X[9] = 93 possibilities
12 => X[10] = 45 possibilities
12 => X[11] = 11 possibilities
12 => X[12] = 1 possibilities
Full output for X[4]

12 => X[4] = [8, 2, 1, 1]
12 => X[4] = [8, 1, 2, 1]
12 => X[4] = [8, 1, 1, 2]
12 => X[4] = [6, 4, 1, 1]
12 => X[4] = [6, 2, 2, 2]
12 => X[4] = [6, 1, 4, 1]
12 => X[4] = [6, 1, 1, 4]
12 => X[4] = [4, 6, 1, 1]
12 => X[4] = [4, 4, 2, 2]
12 => X[4] = [4, 2, 4, 2]
12 => X[4] = [4, 2, 2, 4]
12 => X[4] = [4, 1, 6, 1]
12 => X[4] = [4, 1, 1, 6]
12 => X[4] = [2, 8, 1, 1]
12 => X[4] = [2, 6, 2, 2]
12 => X[4] = [2, 4, 4, 2]
12 => X[4] = [2, 4, 2, 4]
12 => X[4] = [2, 2, 6, 2]
12 => X[4] = [2, 2, 4, 4]
12 => X[4] = [2, 2, 2, 6]
12 => X[4] = [2, 1, 8, 1]
12 => X[4] = [2, 1, 1, 8]
12 => X[4] = [1, 8, 2, 1]
12 => X[4] = [1, 8, 1, 2]
12 => X[4] = [1, 6, 4, 1]
12 => X[4] = [1, 6, 1, 4]
12 => X[4] = [1, 4, 6, 1]
12 => X[4] = [1, 4, 1, 6]
12 => X[4] = [1, 2, 8, 1]
12 => X[4] = [1, 2, 1, 8]
12 => X[4] = [1, 1, 8, 2]
12 => X[4] = [1, 1, 6, 4]
12 => X[4] = [1, 1, 4, 6]
12 => X[4] = [1, 1, 2, 8]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote