tags:

views:

139

answers:

4

Imagine I have this list, that is divided by 3

1
2
3
4
5
6
7
8
9

Now, I have 9 items, grouped in 3 sections.

My question is how know in which section is 6 (ie: 6 belong to section 2, 2 to section 1, 9 to section 3)

+2  A: 

section = ceiling (n / 3)

For example,

ceiling (4 / 3) = ceiling ( 1.33 ) = 2

William Brendel
+4  A: 

Hmmm...... section = ((item-1) / 3) + 1

Igor Brejc
A: 

I'm not totally sure what you're asking, but give this a try:

floor((itemNumber - 1)/numberOfGroups) + 1
Eclipse
A: 

For a list of items divided into sections of size n, the section s of an item i is given by:

s = (i + (n-1)) / n,

where the / is integer division.

So, for your example, item 6 gives (6 + (3-1))/3 = (6+2)/3 = 8/3 = 2.

This applies to many other things as well - I encountered it as "How many nodes do I need to request on a cluster with n CPUs per node?"

Tim Whitcomb