- Home /
Having Trouble with this For Loop
This is quite hard to articulate, but this is what I'm Attempting to do: So I may end up with an array of indexes as:
Currently what I've been trying to do is take some givens: i = current row; (1,2,3...7) r = Square Root of Total Indexes -1 (total indexes will always be a square of 2, a la 64,128, 256...)
i=1, so i+r Once. i=2, i+r Twice. i=3, i+r Thrice.
and so on and so forth...
Answer by Llama_w_2Ls · Feb 12 at 11:50 AM
Well the difference between the next digit in the sequence is always 7, by induction. And you always continue adding 7 to get to the next digit until you've counted n numbers (where n is the row number). Then you reset the counter and start from the digit n - 1. And you repeat. This is the pattern until row number = 8.
So here's the full algorithm:
var numbers = new List<int>();
int rowNum = 1;
int counter = 0;
// First loop
while (rowNum <= 8)
{
int num = rowNum - 1;
numbers.Add(num);
while (counter < rowNum - 1)
{
num += 7;
numbers.Add(num);
counter++;
}
counter = 0;
rowNum++;
}
int offset = 1;
// Second loop
while (rowNum < 16)
{
int num = rowNum - 1 + 7 * offset;
numbers.Add(num);
while (counter < rowNum - offset - 2)
{
num += 7;
numbers.Add(num);
counter++;
}
counter = offset;
rowNum++;
offset++;
}
Your answer
Follow this Question
Related Questions
Finding nearest object with a certain tag without for loops. 1 Answer
How to have a (wait) yield on a for loop? 0 Answers
How do I reset a for loop variable??? 3 Answers
x=x Assignment made to same variable 3 Answers
"for" loop - DrawTexture problem. 0 Answers