alone in Napoli

[This article was first published on R – Xi'an's Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


A combinatorics puzzle from The Riddler about a Napoli solitaire where 4 x 10 cards numbered from 1 to 10 are shuffled and the game is lost when a number (1,2, or 3) is equal to its position modulo 3 (1,2 or 3). A simple R code shows that the probability of winning is around 0.00831:

N=40
for(t in 1:1e6)F=F+!sum(!(sample((1:N)%%10)-(1:N)%%3))

ChatGPT bends over backward to achieve this figure! Now, the exact probability can be found by combinatorics. While there are 40! ways of permuting the 40 cards, those missing the coincidences are

\sum\limits_{k=0}^4\sum\limits_{j=0}^4\sum\limits_{i=0}^4{13\choose i}{13\choose 4-i}{14\choose j}{13-i\choose 4-j}{14-j\choose k}{9+i\choose 4-k}

multiplied by 4!4!4!28! (which I initially forgot), resulting in 0.00831:

for(i in 0:4)for(j in 0:4)for(k in 0:4)
      F=F+exp(lchoose(13,i)+lchoose(13,4-i)+3*lfactorial(4)+
          lchoose(14,j)+lchoose(13-i,4-j)+lfactorial(28)+
          lchoose(14-j,k)+lchoose(9+i,4-k)-lfactorial(40))
To leave a comment for the author, please follow the link and comment on their blog: R – Xi'an's Og.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)