# Le Monde puzzle [#929]

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Xi'an's Og » R**, 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 Le Monde mathematical puzzle:

In the set {1,…,12}, numbers adjacent to i are called friends of i. How many distinct subsets of size 5 can be chosen under the constraint that each number in the subset has at least a friend with him?

**I**n a brute force approach, I tried a quintuple loop to check all possible cases:

case=0 for (a in 1:(12-4)) for (b in (a+1):(12-3)) for (c in (b+1):(12-2)) for (d in (c+1):(12-1)) for (e in (d+1):12) case=case+((b-a<2)&(min(c-b,d-c)<2) &(min(d-c,e-d)<2)&(e-d<2))

which returns 64 possible cases. Note that the second and last loop are useless since b=a+1 and e=d+1, necessarily. And c is either (b+1) or (d-1), which means 2 choices for c, except when e=a+4. This all adds up to

A related R question: is there a generic way of programming a sequence of embedded loops like the one above without listing all of the loops one by one?

Filed under: Books, Kids, R Tagged: Le Monde, mathematical puzzle

To

**leave a comment**for the author, please follow the link and comment on their blog:**Xi'an's Og » R**.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.