4
The Walsh matrix is an interesting fractal matrix with the property that every single value in a Walsh matrix has a value of either -1 or 1. Additionally, the size of a Walsh matrix is always a power of 2.
Because each Walsh matrix is identical to the top-left quarter of the immediately higher-order Walsh matrix, we can create a generalized "infinite Walsh matrix" by considering the limit of the sequence of Walsh matrices of order N, as N goes to infinity. From here on, we will call this infinite generalization the Walsh matrix.
Your task is to build a program that takes the coordinates of a location in the Walsh matrix and returns the value that occurs there.
Your program will accept two integers x
and y
as input (in any form you choose), and output the value that appears in the x+1
th row of the y+1
th column of the Walsh matrix. For example, the input
2 5
will return 1
, as the 3rd row in the 6th column of the Walsh matrix has a value of 1. The input
6 4
will return -1
, as the 7th row in the 5th column of the Walsh matrix has a value of -1. The input
2834719 394802
will return -1
, as the 2384720th row of the 394803rd column has a value of -1.
The shortest code to do this in any language wins.
5I quickly figured out a procedure for determining the value of any cell. Problem is, it only gives the correct answer half of the time. :) – DavidC – 2014-02-14T20:26:11.813
2
print "0 ± 1"
– Doorknob – 2014-02-14T22:05:00.823@DavidCarraher, it seems that if you output
1
you'll do better than 50%. – Peter Taylor – 2014-02-15T09:27:24.5301@Peter Taylor, Great idea, and your solution even saves a few characters. – DavidC – 2014-02-15T13:56:51.340
@PeterTaylor Of course, the limit as you go to infinity is still 50%. – Joe Z. – 2014-02-15T14:34:30.233
1Please add a minimal description of the matrix so that the problem makes sense when wikipedia is down. – Sparr – 2019-10-15T04:37:12.370