I needed to calculate the number of different possible rolls that could arise from rolling K dice, each with N-sides. My definition of roll is that something like {1, 1, 2, 3, 4} is equivalent to {1, 4, 3, 1, 2} (order doesn't matter), but not to {1, 1, 3, 3, 3} (they're not the same set of results). For example: Yahtzee is a game involving rolls of 5 6-sided dice—at least initially, before rerolls—and the number of different rolls is thus 252. The case when N = K leads to OEIS sequence A001700.
If I'm not horribly mistaken, this is given by "(N-1+K) choose (N-1)", or equivalently, "(N+K-1) choose K", which is K ! <: K + N
in J. This leads me to four different tacit representations:
d =: ([ ! [: <: +)
. Simple train, without parentheses, though I need to use a cap.d =: ([ (! <:) +)
. No cap, but parenthesizing the inner hook.d =: (] !&<: +)
. Only a three verb train, but using a Compose. It uses the (<: N) ! <: K + N
version.d =: (([ ! +) * ] % +)
. This one rewrites "C(N+K-1, K)" as "C(N+K, K) * N / (N+K)". It's uglier, but in the 0 dice of 0 sides case, it gives 0 instead of 1, which is arguably a less nonsensical answer.Which of these is the most "J-ish" solution?
Also, the monadic case for all of these is meaningless: 1 0 0 0 0 ...
for the first three and 0 1 1 1 ...
for the fourth. A more logical monad for this verb would be the reflexive, as given by d~
, so would it be better to define this verb as (d~ : d)
?
My preference would be:
d =: ([ (! <:) +)
and to add a monadic option to the dyadic
d =: d~ : ([ (! <:) +) NB. 4 d 5 ( 4 rolls of 5 sided dice : 70 possible combinations)
I would add the comment including sample arguments and an expected purpose to save me time were I to stumble across it later.
Of course, the final version would be the choice if 0 d 0 were to return 0, even if it does look a little more complicated.