-
Algorithm help?
Hello,
What line of codes would work to make the following derivation(with the exact order):
int y[i][j];
y[0][0]=0
y[1][0]=0+4
y[2][0]=0+16
y[2][1]=0+4+16
y[3][0]=0+64
y[3][1]=0+4+64
y[3][2]=0+16+64
y[3][3]=0+4+16+64
y[4][0]=0+256
y[4][1]=0+4+256
y[4][2]=0+16+256
y[4][3]=0+4+16+256
y[4][4]=0+64+256
y[4][5]=0+4+64+256
y[4][6]=0+16+64+256
y[4][7]=0+4+16+64+256
...
..
.
See the pattern? it keep adding itself for every increase in 'i'.
Help?
-
Are you sure that's correct? Why do you have two sections that are y[3][ ]?
Frankly, I don't (yet) see a pattern, but maybe it's because your sample isn't quite right. If you see the pattern of "adding itself for every increase in 'i'", then what is stopping you from coding it?
-
Ahh, just saw the pattern, though I still think there's an error in your sample. Jumping from 0 to 4 to 16 to 32 to 64 does not seem to follow a pattern though. If it's powers of 2, what happened to 2 and 8? If it's powers of 4, why is 32 in there?
-
edited.
Sorry for the error in copying..
-
So powers of 4 it is. Okay, so that gets you your "big" number, i.e. the 0, 4, 16, 64, 256.
Now, try to express each line in terms of i, j and previous lines (except the first one, y[0][0]). For example
Code:
y[0][0] = 0
y[1][0] = y[0][0] + 4**1
y[2][0] = y[0][0] + 4**2
y[2][1] = y[1][0] + 4**2
...
That should help you figure out the algorithm.
-
This is what I have, and it doesn;t work, I'm struggling to get what I want. Some help please?
Code:
int a,b,c,d;
int a_max=4;
int y[50][50];
y[0][0]=0;
//algorithm
for(a=0;a<a_max;a++)
for(b=0;b<pow(pow(4,a),0.5);b++)
{
for(c=0;c<2;c++)
for(d=0;d<2;d++)
{
y[a][b]=pow(4,a+1) + y[c][d];
}
}
for(a=0;a<a_max;a++)
for(b=0;b<pow(pow(4,a),0.5);b++)
{
printf("y[a][b]=y[%d][%d]=%d",a,b,y[a][b]);
getchar();
}
-
nevermind. solved. thanks!
-
Less loops. No pow.
Code:
#define ROWS 5
int i, j, lim, t, f, y[ROWS][1 << ROWS - 2];
int k1 = 0x2AAAAAAc;
int k2 = 0x55555554;
memset(y, 0, sizeof(y));
for (i = 1; i < ROWS; i++) {
lim = 1 << i - 1;
f = 1 << i * 2;
for (t = j = 0; j < lim; j++) {
y[i][j] = t + f;
printf("[%d][%d] %d\n", i, j, y[i][j]);
t = t + k1 & k2;
}
printf("\n");
}
[1][0] 4
[2][0] 16
[2][1] 20
[3][0] 64
[3][1] 68
[3][2] 80
[3][3] 84
[4][0] 256
[4][1] 260
[4][2] 272
[4][3] 276
[4][4] 320
[4][5] 324
[4][6] 336
[4][7] 340
-
It's simply bit-scrambling. The results have the same number of bits set as the values 0 to 15 do, it's just that they are different bits. All you need to do is to swap the bits around.