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.

EDIT: It appears what OP wants is this: divide a given positive integer $n$ into three parts; the parts are to be roughly equal; the first and last parts to be equal.


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.


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

for (var t=0; t < start; t++){
for (var t=0; t < middle; t++){
for (var t=0; t < end; t++){

How could I calculate this?

share|improve this question

closed as unclear what you're asking by Alex Wertheim, Amzoti, Jared, L.G., 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.

Please, before voting to delete, ask yourself, what is unclear about the question, in its current form? –  Gerry Myerson Jan 4 at 2:42
@GerryMyerson I really don't know why it was closed, I don't really see what's so unclear but thankyou anyway :) –  Shannon Hochkins Jan 7 at 1:21

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.