Mathematica, 159 100 87 86 85 bytes
n=3;1-Mean@Sign[##&@@Norm/@({1,0,0,-1}~t~n.Partition[#,2,1,1])&/@{1,-1}~(t=Tuples)~n]
To change n
just change the variable definition at the beginning.
Since it's brute force it's fairly slow, but here are the first eight results:
n P(n)
1 1/2
2 3/8
3 7/32
4 89/512
5 269/2048
6 903/8192
7 3035/32768
8 169801/2097152
The last one already took 231 seconds and the runtime is horribly exponential.
Explanation
As I said it's brute force. Essentially, I'm just enumerating all possible A
and B
, compute the two dot products for every possible pair and then find the fraction of pairs that yielded {0, 0}
. Mathematica's combinatorics and linear algebra functions were quite helpful in golfing this:
{1,-1}~(t=Tuples)~n
This generates all n-tuples containing 1
or -1
, i.e. all possible A
. For n = 3
that is:
{{1, 1, 1},
{1, 1, -1},
{1, -1, 1},
{1, -1, -1},
{-1, 1, 1},
{-1, 1, -1},
{-1, -1, 1},
{-1, -1, -1}}
To compute B
we do almost the same:
{1,0,0,-1}~t~n
By repeating 0
, we duplicate each tuple for each 0
it contains, thereby making 0
twice as likely as 1
or -1
. Again using n = 3
as an example:
{{-1, -1, -1},
{-1, -1, 0}, {-1, -1, 0},
{-1, -1, 1},
{-1, 0, -1}, {-1, 0, -1},
{-1, 0, 0}, {-1, 0, 0}, {-1, 0, 0}, {-1, 0, 0},
{-1, 0, 1}, {-1, 0, 1},
{-1, 1, -1},
{-1, 1, 0}, {-1, 1, 0},
{-1, 1, 1},
{0, -1, -1}, {0, -1, -1},
{0, -1, 0}, {0, -1, 0}, {0, -1, 0}, {0, -1, 0},
{0, -1, 1}, {0, -1, 1},
{0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1},
{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
{0, 0, 1}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1},
{0, 1, -1}, {0, 1, -1},
{0, 1, 0}, {0, 1, 0}, {0, 1, 0}, {0, 1, 0},
{0, 1, 1}, {0, 1, 1},
{1, -1, -1},
{1, -1, 0}, {1, -1, 0},
{1, -1, 1},
{1, 0, -1}, {1, 0, -1},
{1, 0, 0}, {1, 0, 0}, {1, 0, 0}, {1, 0, 0},
{1, 0, 1}, {1, 0, 1},
{1, 1, -1},
{1, 1, 0}, {1, 1, 0},
{1, 1, 1}}
Now, for each possible A
, we want the dot product of each of those possible B
, both with A[1 .. n]
and A[2 .. n+1]
. E.g. if our current A
is {1, 1, -1}
, we want the dot product with both {1, 1, -1}
and with {1, -1, 1}
. Since all our B
are already conveniently the rows of a matrix, we want the two sublists of A
as columns of another matrix, so that we can compute a simple dot product between them. But transposing {{1, 1, -1}, {1, -1, 1}}
simply gives {{1, 1}, {1, -1}, {-1, 1}}
which is just a list of all 2-element cyclic sublists of A
. That's what this does:
Partition[#,2,1,1]
So we compute that and take the dot product with our list of B
. Since we now get a nested list (since each possible A
yields a separate vector), we flatten those with ##&@@
.
To find out if a pair {x, y}
is {0, 0}
we compute Sign[Norm[{x,y}]]
where Norm
gives √(x²+y²)
. This gives 0
or 1
.
Finally, since we now just want to know the fractions of 1
s in a list of 0
s and 1
s all we need is the arithmetic mean of the list. However, this yields the probability of both at least one dot product being non-zero, so we subtract it from 1
to get the desired result.
2Test cases for the first few
n
would be helpful. Also maybe an explicit example of A, B and the two inner products might help. – Martin Ender – 2015-06-07T18:55:20.697If we choose to harcode the integer, does
n=4
count as zero, two or three bytes? Does the output have to be exactlya/b
or would[a b]
, e.g., be allowed? – Dennis – 2015-06-07T21:27:16.430@Dennis It has to be exact. If you hardcode the integer will I only have to change it in one place to change
n
? Otherwise, I think that is not allowed. – None – 2015-06-08T07:02:20.657Yes, my program only uses the integer once to compute a cartesian power. Everything else is derived from the resulting array. – Dennis – 2015-06-08T07:06:23.837