This is a brainteaser that I got from a friend.

The question is what formula is being used in the cells?
If you take a closer look at the picture you will see that i = mod10((p or q)-1) and j = integer(i/10). I have also found out that this formula is used some how: a(n) = n^2+1.
But how is n generated? If you take a look at cell I31, why is n = 1.41421356 there and not an integer? Am I thinking wrong?
Any clues would be appreciated.
Link to larger image: http://i.stack.imgur.com/oaHcY.jpg