1

this is my first time posting on this forum so if this isn't acceptable, please let me know and i'll remove the post.

I'm trying to convert the following Java code into a properly notated mathematical formula. However, my math skills themselves are pretty rusty.

If anyone could help me out with this, I would be very appreciative.

Thanks.

The Code

// Define our two sets that we will be using
byte[] x = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};    
byte[] y = new byte[] {9, 8, 8, 6};

// Define our result set that will have all of the results put into it
byte[] z = new byte[x.length];

// Define two variables, one called xIndex and one called yIndex.
// These will be used to pull data from the two sets
int xIndex = 0;
int yIndex = 0;

// Execute this code over and over until xIndex is the same number as 
// the number of items in set x
while(xIndex < x.length){

    // Create a new entry in the result set z that is equal to the 
    // current number at xIndex in set x + the current number at yIndex in set y
    z[xIndex] = x[xIndex] + y[yIndex];

    // If yIndex is the same number as the number of items in y, set it to 0.
    // Else, increment it by one
    if (yIndex == (y.length - 1)) {
        yIndex = 0;
    }else{
        yIndex = yIndex + 1;
    }

    // Increment xIndex by 1 so that the loop eventually 
    // ends and so that we can grab the next item on 
    // the next iteration of the loop
    xIndex = xIndex + 1;
}



// the set z now holds our new set
// it should look like this
// {10, 10, 11, 10, 14, 14, 15, 14, 18, 8}

1 Answers1

1

Well, say xIndex is $k$.

You are incrementing yIndex with xIndex, and cycling back to $0$ if it exceeds length of y. So yIndex is $k \pmod {len(y)}$.

So it looks like $z_k=x_k+y_{k\pmod 4}$.

KalEl
  • 3,297
  • So, it should actually be 0 if it exceeds 1 less than length of y, as pointed out by @FabioSomenzi above, so would this be correct -> yIndex is k(mod (len(y) - 1)) – Nathan F. Mar 31 '17 at 00:10
  • 1
    @NathanFiscaletti No, it's rather that the complete formula is $\forall k (0 \leq k < x.length \rightarrow z_k = x_k + y_{k \bmod 4})$ or, more succinctly, $\forall, 0 \leq k < x.length ,(z_k = x_k + y_{k \bmod 4})$ . – Fabio Somenzi Mar 31 '17 at 00:19