Modulus? Getting group index from sequence index

Viewed 45

I have a sequence of 64 items. I need to organise them in groups of 4 that are themselves placed in groups of four.

Im looking for an algorythm that would take the seuence index and return the index within its group and the parent groups grup index.

I aparently also would like to learn how to explain these kind of things. Im sure there are some mathematical terms that make more sense. Maybe its better to explain with examples:

Index -> Returns

0 -> 1.1.0      
1 -> 1.1.1      
2 -> 1.1.2      
3 -> 1.1.3      
4 -> 1.2.0      
5 -> 1.2.1      
6 -> 1.2.2      
7 -> 1.2.3      
8 -> 1.3.0      
9 -> 1.3.1  
.......    
60 -> 4.4.0    
61 -> 4.4.1    
62 -> 4.4.2    
63 -> 4.4.3
1 Answers

Assuming that 6 maps to 1.2.2 and 7 maps to 1.2.3 and 9 maps to 1.3.2 then you can use this:

(int First, int Second, int Third) GetSequence(int index)
{
    int first = 1;
    int second = 1;
    int third = 0;

    third += (index % 4);
    second += (index / 4) % 4;
    first+= (index / 16) % 4;

    return (first, second, third);
}
Related