Thread: Algorithm help?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    17

    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?
    Last edited by Zul56; 02-21-2013 at 10:29 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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?

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    17
    edited.

    Sorry for the error in copying..

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    17
    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();
            }
    Last edited by Zul56; 02-22-2013 at 12:08 AM.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    17
    nevermind. solved. thanks!

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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
    Last edited by nonoob; 02-22-2013 at 02:23 PM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which algorithm ?
    By jack_carver in forum C Programming
    Replies: 8
    Last Post: 02-15-2009, 11:41 AM
  2. A* algorithm
    By jmd15 in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2006, 03:08 PM
  3. Algorithm Help
    By golfinguy4 in forum C++ Programming
    Replies: 9
    Last Post: 08-08-2003, 02:19 PM
  4. Need Help for an algorithm.....
    By arvindkr in forum C++ Programming
    Replies: 0
    Last Post: 08-24-2002, 03:45 AM
  5. my grandfather's chess algorithm can beat your grandfather's chess algorithm...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 08-17-2001, 06:52 PM

Tags for this Thread