14
0
Suppose we define an infinite matrix M
, on N^2 -> {0, 1}
(where N
starts from 1
instead of 0
) in this manner:
M(1, 1)
=0
.For every
x > 1
,M(x, 1)
=1
ifx
is prime, and0
otherwise.For every
y > 1
,M(1, y)
= they
th term in theThue-Morse sequence
.For every
x, y > 1
,M(x, y)
=M(x, y-1) + M(x-1, y) mod 2
.
The top-left 16x16
section of this matrix looks like (with x
being rows and y
being columns):
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
1 0 1 1 0 0 0 1 0 0 0 1 1 0 1 1
1 1 0 1 1 1 1 0 0 0 0 1 0 0 1 0
0 1 1 0 1 0 1 1 1 1 1 0 0 0 1 1
1 0 1 1 0 0 1 0 1 0 1 1 1 1 0 1
0 0 1 0 0 0 1 1 0 0 1 0 1 0 0 1
1 1 0 0 0 0 1 0 0 0 1 1 0 0 0 1
0 1 1 1 1 1 0 0 0 0 1 0 0 0 0 1
0 1 0 1 0 1 1 1 1 1 0 0 0 0 0 1
0 1 1 0 0 1 0 1 0 1 1 1 1 1 1 0
1 0 1 1 1 0 0 1 1 0 1 0 1 0 1 1
0 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1
1 1 0 0 1 0 1 1 0 1 1 1 0 1 1 0
0 1 1 1 0 0 1 0 0 1 0 1 1 0 1 1
0 1 0 1 1 1 0 0 0 1 1 0 1 1 0 1
0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 1
Your task is to build a program that will evaluate the value of an arbitrary entry in this matrix as accurately as possible.
Your program will take two integers x
and y
as input, in any form you choose, and return M(x, y)
, which will be either 0
or 1
.
Your code may be written in any language, but must not exceed 64 kilobytes (65,536 bytes) of source code size or 2 MB (2,097,152 bytes) of total memory usage. Your program must start with empty memory (i.e. it cannot load data from somewhere else) and run independently for each input (that is, it may not store common data for multiple runs). Your program must also be able to evaluate all the entries in the top-left 8192x8192
square in a reasonable amount of time.
The program that evaluates the most entries correctly in the top-left 8192 x 8192
square will be the winner, with shorter code acting as a tie-breaker.
I'm probably going to update the testing case to something slightly more elegant in a moment, so hang on with the testing until I edit the question again. – Joe Z. – 2014-03-19T21:25:25.343
@mbuettner Yes, it does. – Joe Z. – 2014-03-19T21:27:34.393
1I fail to see how we need a new tag for "accuracy." This is just a [code-challenge]. Please run new challenge genre ideas through meta first (there's one thing we learned from [code-trolling]). – Doorknob – 2014-03-19T21:31:58.190
^ Noted. I'll remove that tag. – Joe Z. – 2014-03-19T21:34:11.120
You posted a comment on a now deleted answer saying "You need to create a function that computes the value of that matrix with two coordinates without computing the entire matrix." I can't find in your question the "without computing the entire matrix" part. Perhaps you think that the memory constraints enforce that, but perhaps it isn't so. – Dr. belisarius – 2014-03-19T21:59:45.733
Since any valid answer will be "accurate" (or it's buggy), isn't this just code golf? – intx13 – 2014-03-19T22:01:50.117
@intx13: The point is that an answer may produce incorrect results but still be considered valid. An answer that simply says
print 1
would still be right about 50% of the time, but would be beaten by an answer that gave the correct answer 90% of the time. – Joe Z. – 2014-03-19T22:09:44.330@belisarius It would also be prohibitively expensive time-wise to actually test it if you computed the entire matrix at once. The point I was trying to make was that Kaya had created a literal matrix, and not a function that evaluates values in that matrix. – Joe Z. – 2014-03-19T22:11:23.583
@JoeZ I guess. But since I have a solution that is 100% accurate and the challenge goes first by who is most accurate, it's now code golf. – intx13 – 2014-03-19T22:13:23.630
That being said, if you can find a way to encode the entire matrix in just 2 MB of memory, go right ahead. That would count as a 100% accurate answer. – Joe Z. – 2014-03-19T22:13:25.000
I presume that you're of the school of thought that
N
doesn't include0
, but since there are two schools of thought it's worth making that clear. It's also worth making it clear when you first mention them rather than three lines later that for some reason you've chosen to usex
for the row andy
for the column, confusing anyone who's used to Cartesian coordinates. – Peter Taylor – 2014-03-19T23:42:57.100Actually, usually to me
N
does include0
, but for the purposes of this question it didn't work out. And I already put in the latter clarifier aboutx
andy
. Perhaps it would be better to usea
andb
so they're not confused with Cartesian coordinates? – Joe Z. – 2014-03-19T23:46:19.5301@TheDoctor It's not too uncommon. The accepted answer changes over time. – Joe Z. – 2014-03-19T23:47:13.283
@JoeZ. - I usually wait till the interest on the question has declined – TheDoctor – 2014-03-19T23:52:34.033
I believe the memory restriction unnecessarily complicates this. It's also hard to ensure accuracy. – qwr – 2014-03-20T00:09:52.207
@qwr to be honest, I think the memory restriction is the most interesting thing about this problem. just computing the entire table in rows or columns seems quite trivial. – Martin Ender – 2014-03-20T00:25:53.057
r
andc
for row and column? I edited my comment when I saw that you had put in a clarification, but I didn't see it immediately because I read the bullet points and then went straight to the table. – Peter Taylor – 2014-03-20T08:33:31.230