0?0=1
a?b=sum[a?i+i?a|i<-[0..b-1]]
f n=n?n
Try it online!
A fairly direct implementation that recurses over 2 variables.
Here's how we can obtain this solution. Start with code implementing a direct recursive formula:
54 bytes
0%0=1
a%b=sum$map(a%)[0..b-1]++map(b%)[0..a-1]
f n=n%n
Try it online!
Using flawr's rook move interpretation ,a%b
is the number of paths that get the rook from (a,b)
to (0,0)
, using only moves the decrease a coordinate. The first move either decreases a
or decreases b
, keeping the other the same, hence the recursive formula.
49 bytes
a?b=sum$map(a%)[0..b-1]
0%0=1
a%b=a?b+b?a
f n=n%n
Try it online!
We can avoid the repetition in map(a%)[0..b-1]++map(b%)[0..a-1]
by noting that the two halves are the same with a
and b
swapped. The auxiliary call a?b
counts the paths where the first move decreases a
, and so b?a
counts those where the first move decreases b
. These are in general different, and they add to a%b
.
The summation in a?b
can also be written as a list comprehension a?b=sum[a%i|i<-[0..b-1]]
.
42 bytes
0?0=1
a?b=sum[a?i+i?a|i<-[0..b-1]]
f n=n?n
Try it online!
Finally, we get rid of %
and just write the recursion in terms of ?
by replacing a%i
with a?i+i?a
in the recursive call.
The new base case causes this ?
to give outputs double that of the ?
in the 49-byte version, since with 0?0=1
, we would have 0%0=0?0+0?0=2
. This lets use define f n=n?n
without the halving that we'd other need to do.