Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I need a start, middle and end value of any given number, it doesn't have to be equally distributed, just needs a bit of logic. Example:

var parts = 3;

Obviously, I could devide this by 3 and get an even devised result start: 1; middle: 1; end: 1.

However if the user was to put in something like:

var parts = 8;

In my eyes, the answer would be start: 2; middle: 4; end: 2.

I need to perform a basic task depending on the length of the 'parts' variable.

Example:

I need to calculate the start, middle and end values so I can do something like this.

for (var t=0; t < start; t++){
    console.log('Start');
}
for (var t=0; t < middle; t++){
    console.log('Middle');
}
for (var t=0; t < end; t++){
    console.log('End');
}

How could I calculate this?

share|improve this question

closed as unclear what you're asking by AWertheim, Amzoti, Jared, O.L., Danny Cheuk Jun 30 '13 at 7:33

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 1 down vote accepted

So, you are trying to divide a given positive integer $n$ into three parts; you want the parts to be roughly equal; it also seems you'd like the first and last parts to be equal. First divide $n$ by $3$, and call the quotient $q$, and the remainder $r$. Then your desired parts are $q$, $q+r$, $q$. This agrees both with your example of $3\to1,1,1$ and $8\to2,4,2$.

share|improve this answer
    
var parts=9; var start = end = middle = (parts - (parts % 3)) / 3; middle += parts % 3; Like that? :) –  Shannon Hochkins Jun 30 '13 at 6:51
    
I don't know --- I'm fluent in Math, but not in code. –  Gerry Myerson Jun 30 '13 at 6:55
    
It worked :) Thanks dude! –  Shannon Hochkins Jun 30 '13 at 6:58
    
You're welcome. The standard way to thank people here is by clicking the little check mark next to the answer, thereby signifying acceptance of the answer. –  Gerry Myerson Jun 30 '13 at 7:02
    
Done :) Thanks again –  Shannon Hochkins Jun 30 '13 at 7:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.